Why ComponentState need reinstance twice?

I would like to ask a question about rx.ComponentState:
Why do we need use that two:

created_ComponentState = ComponentState.create
instance_created_ComponentState = created_ComponentState()

can run:

class ComponentState(rx.ComponentState):
    var: str = 'TEXT'
    @classmethod
    def get_component(cls):
        return rx.box()
created_ComponentState = ComponentState.create
instance_created_ComponentState = created_ComponentState()
class state(rx.State):
    async def function(self):
        get_state = await self.get_state(created_ComponentState.State)
        print(get_state.var)
def test() -> rx.Component:
    return rx.button('click_me', on_click=state.function)

Why can’t it be like this:
can’t run

class ComponentState(rx.ComponentState):
    var: str = 'TEXT'
    @classmethod
    def get_component(cls):
        return rx.box()
created_ComponentState = ComponentState.create
class state(rx.State):
    async def function(self):
        get_state = await self.get_state(created_ComponentState().State)
        print(get_state.var)
def test() -> rx.Component:
    return rx.button('click_me', on_click=state.function)

it while return error:

raise RuntimeError( RuntimeError: Cannot populate parent states of reflex___state____state.reflex___istate___dynamic____component_state_n3 without redis. (All states should already be available – this is likely a bug).

is it realy a bug?
I seem to think it’s my way of using it that’s not right

When you call ComponentState.create this creates a new state subclass with the component state class being used as a template. So you must save the returned component if you want to access .State for it. If you call ComponentState.create again (and at runtime), it will create a new state subclass that the runtime doesn’t know anything about and isn’t linked to any previously created component state.

1 Like