Variable in rx.link's href #1622
chimeracle
announced in
General
Replies: 1 comment 4 replies
-
A computed var cannot take arguments, it must purely be a function of the state...
The following snippet appears to be working for me, hopefully this helps. from typing import List
import reflex as rx
class State(rx.State):
class_list = ['Class1', 'Class2', 'Class3']
class_link: List[str] = ['/class1', '/class2', '/class3']
def render_class(state, index):
return rx.tr(
rx.td(state.class_list[index]),
rx.td(
rx.link(
"Take the class",
href=state.class_link[index],
border="0.1em solid",
padding="0.5em",
spacing="3em",
border_radius="0.3em",
_hover={
"color": "rgb(107,99,246)",
},
),),
)
def index() -> rx.Component:
return rx.container(rx.center(
rx.vstack(
rx.heading("Classes", font_size="2em"),
rx.box("Your currently available classes "),
rx.link(
"Intro Quiz",
href="/class1a",
border="0.1em solid",
padding="0.5em",
border_radius="0.5em",
_hover={
"color": "rgb(107,99,246)",
},
),
rx.table(
rx.thead(
rx.tr(
rx.th("Class"),
rx.th("Link"),
)
),
rx.tbody(
rx.foreach(State.class_list, lambda data, index: render_class(State, index)),
),
),
spacing="1.5em",
font_size="2em",
),
padding_top="10%",
)
)
# Add state and page to the app.
app = rx.App()
app.add_page(index)
app.compile() |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How would I craft a variable based on a list index and then use that variable in a rx.link's href value? Below in the render_class function, I'm getting tripped up with the error message "ComputedVar" object is not callable. I've tried looking at the documentation's State > Var section and mimicking the computed variable section, but this doesn't appear to be the answer or I'm implementing it wrong. Bonus points if anyone has any clues as to how to integrate a clickable link in rx.datatable based off a dataframe instead as that was my initial idea(hence the aforementioned items in the code).
What am I doing wrong? Bear in mind this is part of an app and I'm adding this page and base state elsewhere, so there's no main method at the bottom.
Beta Was this translation helpful? Give feedback.
All reactions