Inline Components
Describes how to create stateful components within a single function
Last updated
Describes how to create stateful components within a single function
Last updated
MauiReactor also features a nice ability to wrap a stateful component within a function.
Inline components are somewhat inspired by React hooks so you may notice some similarities if you already have experience with them.
For example, consider that we want to create a custom Entry control that works like the Material entry widget:
This is the usual way we would create it in MauiReactor:
MauiReactor allows you to collapse the component + state in a single declaration that you can return from a function:
In the above example, we set the field name to _labelText
instead of the more obvious _label
to avoid name collisions with the component Label()
In Line 2 we use the static method Component.Render() which takes a Func<VisualNode>. It creates a hidden stateful component on the fly and uses the passed function to render its content.
In Line 5 we declare our state using the method UseState<S>() where S is the type of state we need to render the component. You can pass any type to it but, of course, the beauty of the solution is to take everything contained in the function without external references. In the above sample, we use a c# Tuple that has 2 properties IsFocused and IsFilled.
The rest of the function is more or less the same as the content of the Render method.
Inline Components are a perfect choice to render small components that have simple states, you can for example put it in a generic static class that provides general theming functions to the app.