Expand description
Callbacks define a standard way to store functions and closures. They are useful for component properties, because they can be used to define optional callback functions, which generic props don’t support.
§Usage
Callbacks can be created manually from any function or closure, but the easiest way
to create them is to use #[prop(into)]]
when defining a component.
#[component]
fn MyComponent(
#[prop(into)] render_number: Callback<i32, String>,
) -> impl IntoView {
view! {
<div>
{render_number.call(1)}
// callbacks can be called multiple times
{render_number.call(42)}
</div>
}
}
// you can pass a closure directly as `render_number`
fn test() -> impl IntoView {
view! {
<MyComponent render_number=|x: i32| x.to_string()/>
}
}
Notes:
- The
render_number
prop can receive any type that implementsFn(i32) -> String
. - Callbacks are most useful when you want optional generic props.
- All callbacks implement the
Callable
trait, and can be invoked withmy_callback.call(input)
. On nightly, you can even domy_callback(input)
- The callback types implement
Copy
, so they can easily be moved into and out of other closures, just like signals.
§Types
This modules implements 2 callback types:
Use SyncCallback
when you want the function to be Sync
and Send
.
Structs§
- Callbacks define a standard way to store functions and closures.
- A callback type that is
Send
andSync
if its input type isSend
andSync
. Otherwise, you can use exactly the way you useCallback
.
Traits§
- A wrapper trait for calling callbacks.