mirror of
https://github.com/leptos-rs/book.git
synced 2025-12-27 07:35:38 -05:00
Note about extracting an attribute spread (#225)
* Note about extracting an attribute spread into function returning `impl Attribute` * Note about extracting an attribute spread into function returning impl Attribute Simplified the example as per review. --------- Co-authored-by: Greg Johnston <greg.johnston@gmail.com>
This commit is contained in:
@@ -429,6 +429,24 @@ view! {
|
||||
}
|
||||
```
|
||||
|
||||
``````admonish note
|
||||
If you would like to extract the attributes into a function so you can use it in multiple components, you can do so by implementing a function that returns `impl Attribute`.
|
||||
|
||||
That would make the example above look like this:
|
||||
|
||||
```rust
|
||||
fn spread_onto_component() -> impl Attribute {
|
||||
view!{
|
||||
<{..} aria-label="a component with attribute spreading">
|
||||
}
|
||||
}
|
||||
|
||||
view!{
|
||||
<SomeComponent {..spread_onto_component()} />
|
||||
}
|
||||
```
|
||||
``````
|
||||
|
||||
If you want to spread attributes onto a component, but want to apply the attributes to something other than all top-level elements, use [`AttributeInterceptor`](https://docs.rs/leptos/latest/leptos/attribute_interceptor/fn.AttributeInterceptor.html).
|
||||
|
||||
See the [`spread` example](https://github.com/leptos-rs/leptos/blob/main/examples/spread/src/lib.rs) for more examples.
|
||||
|
||||
Reference in New Issue
Block a user