DioxusLabs / DioxusLabs/sdk

Storage module causes console warnings

Open
#71 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
302
Forks
58
PR merge metrics
No merged PRs in 30d

Description

On Dioxus 0.6.2 when using local storage like this

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

...

let mut hist = dioxus_sdk::storage::use_synced_storage::<
dioxus_sdk::storage::LocalStorage,
BTreeMap>,
>("BTreeMap_ServerCallEvent".to_string(), || {
BTreeMap::>::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](https://github.com/user-attachments/assets/a14db708-8d32-4059-8c39-d3c2a5d85853)

## 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> = 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,

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.