#`use_effect` not working

1 messages · Page 1 of 1 (latest)

glossy dome
#

I have a Q. regarding use_effect usage:
In my code here:

#[component]
pub(crate) fn Setup2fa(user_id: String, is_signup: bool) -> Element {
    let mut others = use_context_provider(|| {
        Signal::new(Others { is_page_loading: true, ..Default::default() })
    });

    use_effect(move || {
        if is_signup {
            show_timed_toast(&mut success_toast_details, "New Password Set!".to_string(), None);
            others.write().purpose = OtpFor::Setup2fa;
        } else {
            others.write().purpose = OtpFor::Renew2fa;
        }
    });

others().purpose value is not getting updated based on is_signup value. Am I missing anything? Anyone pls?

#

toast seem to work.

ebon pollen
#

any error in browser console?

glossy dome
#

None

#

This is how it looks .

#

It's weird that

#[component]
pub(crate) fn Setup2fa(
    user_id: String,
    // is_signup: ReadOnlySignal<bool>
    is_signup: bool,
) -> Element {
    let mut others = use_context_provider(|| {
        Signal::new(Others {
            is_page_loading: true,
            purpose: OtpFor::Renew2fa,
            ..Default::default()
        })
    });

    use_effect(move || {
        if is_signup {
            show_timed_toast(&mut success_toast_details, "New Password Set!".to_string(), None);
            info!("is_signup: true");
            others.write().purpose = OtpFor::Setup2fa;
        }
    });

doesn't work as expected.

#

I tried with both is_signup as ReadOnlySignal<bool> & bool types, but neither worked.

ebon pollen
#

maybe use_memo is a better fit?

glossy dome
#

when the page loads, then in the url, "..../true" the true/false value of is_signup is supposed to set the purpose. Will use_effect not fit here?

#

This code worked as expected:

#[component]
pub(crate) fn Setup2fa(user_id: String, is_signup: bool) -> Element {
    let mut others = use_context_provider(|| {
        Signal::new(Others {
            is_page_loading: true,
            purpose: if is_signup { OtpFor::Setup2fa } else { OtpFor::Renew2fa },
            ..Default::default()
        })
    });

    use_effect(move || {
        if is_signup {
            show_timed_toast(&mut success_toast_details, "New Password Set!".to_string(), None);
        }
    });