intendednull / yewdux

Ergonomic state management for Yew applications
https://intendednull.github.io/yewdux/
Apache License 2.0
322 stars 31 forks source link

Questions: Cannot use a selector on a String #64

Closed sybernatus closed 11 months ago

sybernatus commented 1 year ago

I have the following state :

#[derive(Clone, PartialEq, Eq, Deserialize, Serialize, Store, Default)]
#[store(storage = "local", storage_tab_sync)]
pub struct RootState {
    pub(crate) id_token: String,
    pub(crate) other: String
}

I'm trying to use the use_selector function in a yew component on my id_token.

let id_token = use_selector(|state: &RootState| state.id_token.clone());
info!("id_token {}", *id_token);

I have the following error :

error[E0614]: type `impl yew::functional::hooks::Hook<Output = Rc<std::string::String>> + '_` cannot be dereferenced

I've tried multiple things but I can't find the way to display my id_token in the log or even in the Html. Any recommendation to fix this issue?

intendednull commented 1 year ago

What does the surrounding code look like? Selectors are only supported in functional components

christophbeberweil commented 11 months ago

In a functional component you could write it like this:

let id_token: Rc<String> = use_selector(|state: &RootState| state.id_token.clone());
info!(format!("id_token {}", &id_token));

Outside (and inside) of a yew component you can query the state like this:

let root_state_dispatch = Dispatch::<RootState>::new();
let root_state: Rc<RootState> = root_state_dispatch.get();
info!(format!("id_token {}", &root_state.id_token));
sybernatus commented 11 months ago

Thanks @christophbeberweil & @intendednull it works.

I was effectively using the functional pattern in a yew component.