Skip to content

Capture value, id or name of a button or link #2758

Closed Answered by masenf
Gelraiser asked this question in Q&A
Discussion options

You must be logged in to vote

Would something like this work for you?

import random

import reflex as rx


class State(rx.State):
    values: list
    last_click: int

    def on_load(self):
        self.values = [random.randint(1, 100) for _ in range(10)]

    def handle_button_press(self, value):
        self.last_click = value


def index() -> rx.Component:
    return rx.center(
        rx.vstack(
            rx.text("Last clicked: ", State.last_click),
            rx.hstack(
                rx.foreach(
                    State.values,
                    lambda value: rx.button(
                        value,
                        on_click=State.handle_button_press(value),
                    ),
               …

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@masenf
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Gelraiser
Comment options

Answer selected by masenf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants