Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of reactive recursive tree component #465

Open
godstail opened this issue Aug 9, 2022 · 2 comments
Open

Add an example of reactive recursive tree component #465

godstail opened this issue Aug 9, 2022 · 2 comments
Labels
A-examples Area: examples C-enhancement Category: new feature or improvement to existing feature good first issue Good for newcomers

Comments

@godstail
Copy link

godstail commented Aug 9, 2022

Something like:

#[derive(Debug, Clone, PartialEq, sycamore::Prop)]
pub struct MyNode<'a> {
    text: &'a Signal<String>,
    leafs: &'a Signal<Vec<MyNode<'a>>>,
}

#[component]
pub fn MyNodeComp<'a, G: Html>(
    cx: Scope<'a>, 
    node: MyNode<'a>) -> View<G> 
{
    view! { cx,
        div {
            div { ( node.text.get() ) }
            input(bind:value = node.text)
            Indexed {
                iterable: node.leafs,
                view: |cx, leaf| view! { cx, MyNodeComp(leaf) }
            }
        }
    }
}

At the time of 0.8.0-beta.7 this doesn't compile due to lifetime inference error.

@godstail godstail added the C-enhancement Category: new feature or improvement to existing feature label Aug 9, 2022
@godstail
Copy link
Author

godstail commented Aug 9, 2022

RcSignal with create_ref() work:

#[derive(Debug, Clone, PartialEq, sycamore::Prop)]
pub struct MyNode {
    text: RcSignal<String>,
    leafs: RcSignal<Vec<MyNode>>,
}

#[component]
pub fn MyNodeComp<G: Html>(
    cx: Scope, 
    node: MyNode
) -> View<G> 
{
    let text_clone_ref = create_ref(cx, node.text.clone());
    let leafs_ref = create_ref(cx, node.leafs);
    
    view! { cx,
        div {
            div { ( node.text.get() ) }
            input(bind:value = text_clone_ref)
            Indexed {
                iterable: leafs_ref,
                view: |cx, leaf| view! { cx, MyNodeComp(leaf) }
            }
        }
    }
}

@lukechu10 lukechu10 added the A-examples Area: examples label Aug 10, 2022
@godstail
Copy link
Author

With Rust 1.63 first component now compiles with a trick of additional lifetime 'b: 'a.

#[derive(Debug, Clone, PartialEq, sycamore::Prop)]
pub struct MyNode<'a> {
    text: &'a Signal<String>,
    leafs: &'a Signal<Vec<MyNode<'a>>>,
}

#[component]
pub fn MyNodeComp<'a, 'b: 'a, G: Html>(
    cx: Scope<'a>, 
    node: MyNode<'b>) -> View<G> 
{
    view! { cx,
        div {
            div { ( node.text.get() ) }
            input(bind:value = node.text)
            Indexed {
                iterable: node.leafs,
                view: |cx, leaf| view! { cx, MyNodeComp(leaf) }
            }
        }
    }
}

@lukechu10 lukechu10 changed the title Add an example of reactive recursive component Add an example of reactive recursive tree component Oct 7, 2024
@lukechu10 lukechu10 added the good first issue Good for newcomers label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-examples Area: examples C-enhancement Category: new feature or improvement to existing feature good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants