Comments (4)
Was putting together this example and got distracted. Rather than using children
just pass things as normal props. Here is an example that supports either dynamic or not.
#[component]
pub fn WrapsChildren(#[prop(into)] nodes: MaybeSignal<Vec<View>>) -> impl IntoView {
let nodes = move || {
nodes
.get()
.into_iter()
.map(|view| view! { <li>{view}</li> })
.collect::<Vec<_>>()
};
view! {
<ul>{nodes}</ul>
}
}
#[component]
pub fn App() -> impl IntoView {
let providers = &[
"Twitter", "Github", "Discord", "Twitter", "Github", "Discord",
];
let buttons = move || {
providers
.iter()
.map(|provider| {
view! {
<div>
{provider.to_string()}
</div>
}
.into_view()
})
.collect::<Vec<_>>()
};
view! {
<WrapsChildren nodes={Signal::derive(buttons)}/>
<WrapsChildren nodes={["A", "B", "C"].into_iter().map(IntoView::into_view).collect::<Vec<_>>()}/>
}
}
from leptos.
This is working as designed and is the only reasonable implementation given the way the framework works.
from leptos.
In this case how can you create a dynamic number of items where each item is decorated separatelly by the component ?
from leptos.
Just to expand a little for anyone reading this in the future...
This is a genuine drawback of fine-grained reactive UI relative to something like a virtual DOM — Since we only run component functions once to set up the reactive system, the only thing the component function knows about a dynamic child is "this is a dynamic child", because this line only runs once:
let children = nodes
.into_iter()
The solution in general is to pass around data, or to pass around more specific data structures. Where the pattern in React might be to compose everything through components and child components, sometimes we'll end up passing props with a little more type information.
There are clear trade-offs here but I think it's worth it.
from leptos.
Related Issues (20)
- `0.7-beta` Action prematurely cancelled when called within `Effect` HOT 3
- Suspense renders fallback and children on page refresh HOT 6
- `contextmenu` event expects a `MouseEvent` but should accept a `PointerEvent` HOT 2
- Can some documentation regarding web components be added? HOT 4
- Panic with Effect inside nested Suspense in outsite component (tested on `0.7-beta`) HOT 2
- Panic with closure and callbacks in nested Suspense HOT 1
- Island props (default / into)
- Feauture Request: Stream-managed state signal HOT 7
- `<img />` has missing attributes HOT 1
- 0.7 todos
- The on_cleanup function has not been triggered correctly
- ArcAsyncDerived::new_with_initial() setting multiple dependencies will *NOT* hold until the latest change is ready HOT 3
- `leptos_axum::redirect` fails if there is a delay issuing it inside a server function HOT 3
- `ActionForm` with complex args does not work with javascript disabled HOT 2
- Inconsistency in `aria-current="page"` behavior in `<A>`components HOT 1
- Rendering a component in a ProtectedRoute causes resources to be loaded twice
- Content inside of declarative shadow dom is not rendered HOT 5
- Bug: Unexpected Server-Side Resource Evaluation in Leptos 0.7-beta HOT 1
- Inconsistency in client-side query fragment handling HOT 3
- use:directive syntax doesn't work with generic params in Leptos 0.7 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leptos.