DioxusLabs / DioxusLabs/dioxus

Writable memos break consistency guarantees

Open
#4,800 0 comments 1 reaction 1 assignee Claimed by @ealmloff View on GitHub
bug signals
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

You can set the value of a memo and immediately read back a different value.

The 0.6 version of use_memo has strong consistency guarantees about its value. When you read the value, it should be the same as running the function the memo caches. Allowing the value to be written to breaks that guarantee and results in some very unexpected behavior.

I do think there is value in a hook like a `use_signal` that resets the value when a dependency of the initialization closure changes, but the behavior of that hook is different from memos. Solid does this with a separate [writable memo function](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createwritablememo)

**Steps To Reproduce**

Steps to reproduce the behavior:

- Run this code, observe a panic

```rust
use dioxus::prelude::*;

fn main() {
dioxus::launch(app);
}

fn app() -> Element {
let mut count = use_signal(|| 0);
let mut doubled = use_memo(move || count() * 2);
use_effect(move || {
// At the start of the effect, count is 0 and doubled is 0
// Set the count value to 1
count.set(1);
// Set the memo value to 1
doubled.set(1);
// The memo value isn't 1
assert_eq!(doubled, 1);
});

rsx! {}
}
```

**Expected behavior**

The value of the memo you read should be the value you set or you should not be able to write to memos

**Environment:**

- Dioxus version: main
- Rust version: nightly
- OS info: macOS
- App platform: all

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.