Decorator turning a render function into a Component.
Some components do not have internal state, and these components are often little more than a render function. Creating a Component class results in a lot of boiler plate code:
class MySimpleComp(Component): @register_props def __init__(self, prop1, prop2, prop3): super().__init__() def render(self): # Only here is there actual logic.
To cut down on the amount of boilerplate, you can use the make_component decorator:
@make_component def MySimpleComp(self, prop1, prop2, prop3, children): # Here you put what you'd normally put in the render logic # Note that you can access prop1 via the variable, or via self.props return View()(Label(prop1), Label(self.prop2), Label(prop3))
Of course, you could have written:
# No decorator def MySimpleComp(prop1, prop2, prop3): return View()(Label(prop1), Label(self.prop2), Label(prop3))
instead. The difference is, with the decorator, an actual Component object is created, which can be viewed, for example, in the inspector. If this component uses State Values or State Managers, only this component and (possibly) its children will be re-rendered. If you don’t use the decorator, the returned components are directly attached to the Component that called the function, and so any re-renders will have to start from that level.
f – the function to wrap. Its first argument must be self, and children must be one of its parameters.