Callbacks (deprecated) ..TODO
Easy, isn't it?
Callbacks can sometimes be tricky in Leptos. When realized with generics, they are easy to use as a component-consumer and fast, but hard(er) to write as an author and, because of their generic nature, increase WASM binary size.
If the property of a component specifying a callback should be
An additional burden with the generic approach:
If you, as a component author, need to move a callback into more places, you can put it inside a call to
- (a) components are not often recreated, as we always want to reactively updated their content instead
- (b) some callbacks aren't called often, as they are only used to propagate user input and users are incredibly slow compared to machines.
These make both the performance hit of creating a Box<dyn Fn> and calling it later somewhat irrelevant. The penalty of managing an extra StoredValue through Leptos should not be overlooked though.
Because creating boxed closures manually and storing them in is not ergonomic, Leptonic provides an easy to use
This callback type is both Clone and Copy and can be used at multiple locations in a component without requiring an explicitly Clone/Copy closure to be provided by the user of the component.
Some Leptonic components use this type for some of their callbacks when the generic approach was not ergonomic. You can also use the Callback type in your own components props. For example:
As seen in the definition. Two generic arguments are available. Use a tuple for T if the callback should receive multiple values. Provide the second type, otherwise defaulting to (), when the callback should return a value.
Create it when instantiating your component using the
Calling a callback in a component is as simple as this:
Passing the callback further down to children is no problem.
There are two drawbacks though:
- Callbacks cannot take (non-static) references, only owned values.
- If a child requires a callback of slightly changed signature, you have to pay for creating an intermediate callback..