Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ClientState compatibility with rx.input #3490

Merged
merged 4 commits into from
Jun 19, 2024
Merged

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Jun 13, 2024

Tweaks to make the ClientState work better with text input fields

import reflex as rx


class AppState(rx.State):
    items: list[str]

    def add_item(
        self,
        value: str,
    ):
        self.items.append(value)


def todo_list():
    ItemState = rx._x.client_state(default="")

    return rx.vstack(
        rx.unordered_list(
            rx.foreach(
                AppState.items,
                lambda item: rx.list_item(rx.text(item)),
            ),
        ),
        rx.hstack(
            ItemState,
            rx.input(
                placeholder="what to do",
                id="input",
                value=ItemState.value,
                on_change=ItemState.set_value,
            ),
            rx.button(
                "Add",
                on_click=AppState.add_item(ItemState.value),
            ),
        )
    )


def index() -> rx.Component:
    # Welcome Page (Index)
    return rx.container(
        rx.color_mode.button(position="top-right"),
        todo_list(),
        rx.logo(),
    )


app = rx.App()
app.add_page(index)

@masenf masenf merged commit ffb24ce into main Jun 19, 2024
47 checks passed
@masenf masenf deleted the masenf/client-state-fixes branch June 25, 2024 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants