[Question] How to propagate events or state between user controls #1907
-
QuestionSuppose that you have three user controls:
and basically the hierarchy is that the parent holds both ChildA and ChildB. Child A has a button that will update it's state when clicked, similarly ChildB has a text that will display the current state as seen in the code example. The state is basically instantiated in the Parent component and passed down to children. Can childA modify change and affect the state in either the Parent or ChildB components? Is there a way to achieve this using flet? So far I've found only PubSub and Client/Session storage but I don't think that will suit my purpose because I am dynamically generating the components based on user input. Code sampleclass ChildA(ft.UserControl):
def __init__(self, state):
super().__init__()
self.state = state
def button_clicked(self, e):
self.state = 'button clicked'
def build(self):
return ft.TextButton(text="Click me", on_click=self.button_clicked)
class ChildB(ft.UserControl):
def __init__(self, state):
super().__init__()
self.state = state
def build(self):
return ft.Text(self.state)
class Parent(ft.UserControl):
def __init__(self):
super().__init__()
self.state = 'button not clicked'
def build(self):
return ft.Column(
controls =[
ChildA(self.state),
ChildB(self.state)
]
) Error messageNo response ------------------------------------------------------
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Create some |
Beta Was this translation helpful? Give feedback.
Create some
State
class, keep its instance insideParent
control, pass the instance ofState
class to descendants, have "callback" variables if you need to "call" the parent - nothing special :) You have a correct thinking - from "childB" you notify parent and parent updates itself and "childB" - state changes should be propagated from top to bottom.