How to implement stateful, reusable components? #2592
Replies: 3 comments 3 replies
-
This is a possible use case, but reflex currently does not support it very well. A potential option is to have |
Beta Was this translation helpful? Give feedback.
-
Thank you for the great suggestion. I was using mixins as a workaround, to at least reuse behavior, but I'll definitely try this approach. Let me leave one more bit of info if someone else is attempting to do the same. (Presumably) due to the way handlers are referenced by class ModalState(rx.State):
def show(self):
... # generic modal logic here
self._on_show()
def _on_show(self): #hook for custom logic in the subclasses
pass
class CreateUserState(ModalState):
def _on_show(self):
print("hook for custom logic")
def myComponent() -> rx.Component:
...
rx.button("click",
on_click=CreateUserState.show) I would expect that clicking on the As a workaround, I explicitly implement |
Beta Was this translation helpful? Give feedback.
-
I also meant to link this earlier #1877 |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm trying to implement a generic modal component that can be specialized for different use cases. The modal component knows how to show/hide, has a close button and can display error messages. Individual components can contain forms, ask to confirm actions etc.
I was thinking to define a
@modal
wrapper to decorate individual components and aModalState
"superclass", like:CreateUserState
would inherit fromModalState
, which holds state and accessors (e.g.is_visible
) owned by the modal, and add its own logic.The problem I'm facing is that -in my understanding- the same
ModalState
is shared across all its children, soCreateUserState(ModalState)
andDeleteUserState(ModalState)
share the sameis_visible
value, which is obviously not desired.Is such use case supported? What is the recommended way to implement components that can reuse front-end as well as state logic?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions