Skip to content

Passing state update function to child components #487

Answered by lukechu10
yerlaser asked this question in Q&A
Discussion options

You must be logged in to vote

Try moving in the update function instead of borrowing it. To do so, you'll unfortunately need to add an extra generic to prop struct like so:

#[derive(Prop)]
pub struct ButtonProps<'a, F>
where
    F: Fn(i32),
{
    pub updater: F,
    pub action: &'a i32,
}

This way you won't have any borrowing issues.

However, if you really want to borrow the update function, the problem is that the closure itself lives on the stack which does not last long enough. It needs to be moved onto the heap so that it may live as long as the reactive scope ('a). The simplest way to do so would be to add the following to main.rs

let update = create_ref(cx, update)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@lukechu10
Comment options

@yerlaser
Comment options

Answer selected by yerlaser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants