Initializing function.
Please help me make an initializing function.
I'm using the solara.use_router().search function to get a data. For example, I use this data as values in solara.Select.
I have a problem that after changing the value in select and clicking the button, the value returns to the one I set during initialization.
Therefore, I want to write a function that will be called only once when first launched, receive data, write it to several solara.reactive (I have a lot of them), and then not run.
8 Replies
In a component you can use https://solara.dev/documentation/api/hooks/use_effect
If you use it for the intial state of use_reactive, you can also consider using https://solara.dev/documentation/api/hooks/use_memo
Hope this helps, otherwise please share the code.
Regards,
Right now my code looks like this. And I don’t know how to separate initialization using data into a separate function.
if you put all that initialization code in a function that gets called by use_effect, it will only be called once, does that make sense?
Yes it does, but for some reason it didn't work out for me. I made this code:
It seems that the "start" function is not called because no one reactive variable is filled.
you should do
use_effect(start, dependencies=[])
otherwise it will stil run on each render. I guess looking at the rest of the code you might want to do the following for correctness (although I don't think query_string can change in your case):
solara.use_effect(lambda: start(query_string), dependencies=[query_string])
Hmm, thank you. I tried to rewrite my code here. And it turns out that my error is slightly different:
Can you please take a look at it, it seems like I'm trying to use the Switch incorrectly.
you cannot use use_reactive in a loop like this, you should use solara.reactive().
Also use_effect executes after the function returns, so c_values.values is still empty, you could use use_memo in this case:
Run and edit this code snippet at PyCafe
Thank you very much. Yes, it seems to be working now, I'll go check everything 🙂