Skip to content

Store a Component in State? (& Higher-Order Components) #1771

Answered by ealmloff
kalzoo asked this question in Q&A
Discussion options

You must be logged in to vote

You were pretty close on the first try. Components cannot be closures (if this is an issue you can work around it with a wrapper component that takes a Fn component and calls it). You can use higher bounded lifetimes to get rid of the 'e lifetime in ModalContext. Here is a working example:

use dioxus::prelude::*;

fn main() {
    dioxus_desktop::launch(app);
}

/// An element builder is a function that takes a scope of any lifetime and returns an element with the same lifetime.
type ElementBuilder = for<'e> fn(Scope<'e>) -> Element<'e>;

#[derive(Default)]
pub struct ModalContext {
    pub open_modals: Vec<ElementBuilder>,
}

impl ModalContext {
    pub fn open_modal(&mut self, modal: Ele…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kalzoo
Comment options

Answer selected by kalzoo
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