Recipe to set Select values dynamically? #3463
thomastiotto
announced in
Q&A
Replies: 1 comment
-
@thomastiotto take a look at our select docs to see how to bind your select to state. Your code can be simplified to be class State(rx.State):
values : list[str] = []
def load_values(self):
self.values = ["a","b"] and rx.page(on_load=State.load_values)
def symbols() -> rx.Component:
return rx.select(State.values) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to implement a Select component whose values are not hardcoded but loaded dynamically.
To this end I have a
State.get_values()
function that generates these values and writes them toState._values
. As I couldn't get the Select to work as I would have expected (i.e.rx.select(State._values)
) I tried to define aread_values()
function that returns theState._values
variable.My frontend code is as follows:
while my backend is like this:
No matter what I try I can't get the Select to show the values that I have loaded into the State. By debugging I see that
read_values()
is called before the page loads and thatState.get_values
is called after that, but then I can't getread_values()
to be called again, so it makes sense that the component is displaying[]
.Am I approaching this the correct way or is there another way or dealing with this kind of problem?
EDIT:
I finally managed to get it to work by changing my code this way, even though I'm not sure what
@rx.cached_var
is doing:frontend:
while my backend is like this:
Beta Was this translation helpful? Give feedback.
All reactions