Skip to content

Writable memos break consistency guarantees #4800

@ealmloff

Description

@ealmloff

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

Steps To Reproduce

Steps to reproduce the behavior:

  • Run this code, observe a panic
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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingsignalsRelated to the signals crate

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions