Giter Club home page Giter Club logo

Comments (4)

gbj avatar gbj commented on August 19, 2024 1

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.

gbj avatar gbj commented on August 19, 2024

This is working as designed and is the only reasonable implementation given the way the framework works.

from leptos.

gzp79 avatar gzp79 commented on August 19, 2024

In this case how can you create a dynamic number of items where each item is decorated separatelly by the component ?

from leptos.

gbj avatar gbj commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.