dioxus-std icon indicating copy to clipboard operation
dioxus-std copied to clipboard

Storage module causes console warnings

Open gabriel-v opened this issue 11 months ago • 1 comments

On Dioxus 0.6.2 when using local storage like this

#[component]
fn NavbarLayout() -> Element {

   ...

    let mut hist = dioxus_sdk::storage::use_synced_storage::<
        dioxus_sdk::storage::LocalStorage,
        BTreeMap<String, VecDeque<ServerCallEvent>>,
    >("BTreeMap_ServerCallEvent".to_string(), || {
        BTreeMap::<String, VecDeque<ServerCallEvent>>::new()
    });

   ...
}

we get the following warning spammed in the browser:

WARN 

/home/gabriel/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-signals-0.6.2/src/warnings.rs:62 

Write on signal at

/home/gabriel/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-sdk-0.6.0/src/storage/mod.rs:627:24 

happened while a component was running. Writing to signals during a render can cause 
infinite rerenders when you read the same signal in the component. Consider writing 
to the signal in an effect, future, or event handler if possible.

The warning appears twice per URL change

Image

User workaround

Moving all the storage signals in the main App component (just before mounting the Route) reduces the warnings to only one per signal per page refresh.

Doesn't work if the storage signals need to change depending on the page, through, they will be printing a lot.

Possible fix?

The easiest way to work around this warning is to use a coroutine that writes to the signal instead of writing to the signal directly.

e.g.

    let mut signal: Signal<Option<Thing>> = use_signal(move || None);
    let coro = use_coroutine(move |mut rx | {
        async move {
            use futures_util::StreamExt;
            while let Some(x) = rx.next().await {
                signal.set(Some(x));
            }
        }
    });

... and in the offending code that writes the value:

coro.send(thing)

Maybe this is something that would work here, since I guess the storage API will be async anyway, right?

Thanks,

gabriel-v avatar Feb 05 '25 20:02 gabriel-v

Thanks for the workaround! Another one is to store the signal returned from the storage hook in a context.

matous-volf avatar Feb 06 '25 16:02 matous-volf