Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback not rendering when vector is passed as dep #3307

Open
ThomasP1988 opened this issue Jun 19, 2023 · 2 comments
Open

callback not rendering when vector is passed as dep #3307

ThomasP1988 opened this issue Jun 19, 2023 · 2 comments
Labels

Comments

@ThomasP1988
Copy link

ThomasP1988 commented Jun 19, 2023

Hi,
I'm trying to pass a state as dependency for a callback function, this state is a vector of struct, I get no compiler error, but the callback never update when the vector state is mutated.

Am I doing something wrong or is there a workaround to this problem?

thanks for your help.

let messages_handler: yew::UseStateHandle<Vec<Message>> = use_state(|| {
        vec![]
    });
    let messages_handler_value = (*messages_handler).clone();

    let on_new_message: Callback<Message, ()> = {
        let messages_handler = messages_handler.clone();
        let messages_handler_value = messages_handler_value.clone();

        use_callback(
            move |message: Message, messages_handler_value| {
                let mut messages_handler_value = messages_handler_value.clone();
                messages_handler_value.push(message);

                messages_handler.set(messages_handler_value);
            },
            messages_handler_value.clone(),
        )
    };
@ThomasP1988
Copy link
Author

ThomasP1988 commented Jun 20, 2023

I found a temporary solution by using a buffer

   let message_received_buffer_handle: UseStateHandle<Option<Message>> = use_state(|| None);
    let message_received_buffer_value = (*message_received_buffer_handle).clone();

    let messages_handler: yew::UseStateHandle<Vec<Message>> = use_state(|| vec![]);
    let messages_handler_value = (*messages_handler).clone();

    let on_new_message = {
        let message_received_buffer_handle = message_received_buffer_handle.clone();

        use_callback(
            move |message: Message, message_received_buffer_handle| {
                message_received_buffer_handle.set(Some(message));
            },
            message_received_buffer_handle,
        )
    };

    let connect = use_message_stream(UseMessageStreamProps {
        on_new_message: on_new_message,
    });

    {
        let  mut messages_handler_value = messages_handler_value.clone();
        let messages_handler = messages_handler.clone();

        use_effect_with_deps(
            move |message_buffered| {
                let message = match message_buffered {
                    Some(message) => message,
                    None => return,
                };
                
                match messages_handler_value.last() {
                    Some(last_message) => {
                        if last_message == message {
                            return;
                        }
                    }
                    None => (),
                };

                messages_handler_value.push(message.to_owned());
                messages_handler.set(messages_handler_value)
            },
            message_received_buffer_value,
        );
    }

@schvv31n
Copy link
Contributor

schvv31n commented Oct 19, 2023

Is this actually a bug? I think what happened here is that the line

let messages_handler_value = (*messages_handle).clone();

defines not a state handle, but a Vec, you've simply cloned the inner vector so there's no longer any association of it with the handle.
I'd consider closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants