How to Toggle Properties That Don't Have Built-in Methods

Hey there Retool Community!

I've seen a lot of questions come up from people wanting to change the value of a property in Retool that doesn't have a native JS method (i.e. text1.setValue() ) When it comes to setting properties for Retool components, you might be tempted to do something like this (see bottom panel).

:rotating_light:This will not work! :rotating_light:

In order to set properties dynamically, you will need to reference whatever is changing the property (a query, or some other component) within the right-hand panel. Here's an example using a checkbox component's .value property! Checking and unchecking will yield true or false and allows the component to be disabled/enabled.

Now, let's expand that idea using a JS query and some simple logic in order to use a button to hide a container.

Our JS query will look like this (you can copy and paste):

if(container1.hidden === true){
  return false
}
else if(container1.hidden === false){
  return true
}

Here it is in the editor:

Now I'll attach this query to a button component's "Click" event handler.

And, reference this query.data (in this case hideContainer.data) in the container's "Hidden" property in the right-hand panel.

This may seem similar to the checkbox (it is!), with the only difference being that your JS query could handle more complicated logic. Let's say you want to show this container only if a form is completely filled out or after an API returns only certain values...or whatever you want!

These are just some simple examples. If you have any further questions please let us know! You can respond below or @me or whatever you like. :grinning_face_with_smiling_eyes: And, if you have any examples along these lines that you think would be helpful for other users to see (or just some really cool apps or code you want to showcase), please share below!

Finally, here are some great additional resources from our docs!

5 Likes

A good technique that is essentially the same as using a transformer, but you get the advantage of not needing to wrap every Retool reference in a {{ }}, and I imagine you can probably use async pattern
to get synchrouous flow (ironic isn't it?) if needed.

This does beg the question, are there any other differences between using a js query vs a js transformer for this? Performance differences? Load time differences?

1 Like

Another technique is to use a temp state variable to hold the property's value.

The one advantage of this method is you can set your property from in many parts of your code. Like setting within an event:

Or from a JS query somewhere (or many somewheres):

Any others out there?

The disadvantage of this is there is no good way to find every place you modified the property which can lead to hidden effects. But that is a standard problem with all such platforms.

I will write another tip on how I do track this down.

3 Likes