Skip to content

How to pass state down through context? #13

Closed Answered by ChocolateLoverRaj
ChocolateLoverRaj asked this question in Q&A
Discussion options

You must be logged in to vote

I got it to work. @yishn am I doing this correctly?

use wasm_bindgen::JsValue;
use wasm_react::hooks::{use_context, use_state};
use wasm_react::{clones, export_components, h, Callback, Component, ContextProvider, VNode};
mod counter;

use wasm_react::{create_context, hooks::State, Context};

thread_local! {
  pub static STATE_CONTEXT: Context<Option<State<i32>>> = create_context(None.into());
}

pub struct App;

impl Component for App {
    fn render(&self) -> VNode {
        let state = use_state(|| 0);
        ContextProvider::from(&STATE_CONTEXT)
            .value(Some(Some(state).into()))
            .build(SubComponent {}.build())
    }
}

pub struct SubComponent;

impl Component for 

Replies: 2 comments 2 replies

Comment options

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

Comment options

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

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