Skip to content

[WIP] PoC Dynamic+global state - #3671

Closed
benedikt-bartscher wants to merge 5 commits into
reflex-dev:mainfrom
benedikt-bartscher:dynamic-state
Closed

benedikt-bartscher wants to merge 5 commits into
reflex-dev:mainfrom
benedikt-bartscher:dynamic-state

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Jul 14, 2024 •

Copy link
Copy Markdown
Contributor

Just a small poc to evaluate if it's possible and what needs to be done
currently only works with redis state manager

import asyncio

import reflex as rx


class UserState(
    rx.State,
    # _global=True,
):
    uid: int = 0

    def increment(self):
        self.uid += 1

    @rx.background
    async def increment_task(self):
        while True:
            await asyncio.sleep(2)
            async with self:
                self.uid += 1


def index() -> rx.Component:
    return rx.box(
        rx.foreach(
            ["test", "blubb"],
            lambda x: rx.box(
                rx.text(f"UserState[{x}].uid: {UserState[x].uid}"),
                rx.button(
                    f"UserState[{x}].increment",
                    on_click=UserState[x].increment,
                ),
                rx.button(
                    f"UserState[{x}].increment_task",
                    on_click=UserState[x].increment_task,
                ),
            ),
        ),
    )


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

TODOs:

  • fix typing for __class_getitem__ hack. maybe use smth like State.parametrize(x) instead?
  • try dynamic react contexts, reduce re-rendering
  • what about substates of parametrized states? block this for now?
  • memory state manager
  • dynamic ComponentState
  • either drop _global or implement redis pubsub
  • state key/parameter validation or hashing?
  • testing
  • cleanup and docs

@benedikt-bartscher benedikt-bartscher changed the title [WIP] PoC Dynamic state [WIP] PoC Dynamic+global state Jul 18, 2024
@abulvenz abulvenz mentioned this pull request Jul 18, 2024
3 of 6 tasks
@masenf

masenf commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator

Thank you for contributing to Reflex. I am closing this old or stale PR for now, but please refresh and reopen it if you believe this issue is still relevant.

The new rx.SharedState feature can be used to link all clients to the same "token" to achieve app-global state across all users.

@masenf masenf closed this Mar 6, 2026
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.

2 participants