Comments (5)
Here is a smaller reproduction of the issue:
#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
launch(app)
}
fn app() -> Element {
let mut count = use_signal(|| 0);
let memorized = use_memo(move || count());
let mut effect_run_count = use_hook(|| CopyValue::new(0));
// This effect should only run once, because the memo only ever reads as 1
use_effect(move || {
// trigger a sync update on memo. This will mark the effect as dirty
if *count.peek() == 0 {
count += 1
}
println!("{memorized}");
effect_run_count += 1;
assert!(effect_run_count() <= 1, "This effect should only run once");
});
rsx! { "hello world" }
}
Memo<T>
is recomputing the value when you read the memo inside the effect. Recomputing the value sets the signal with the memorized value which causes all reactive closures that read the memo to rerun, including the effect that it recomputed the value inside.
We can fix this issue by unsubscribing to all signals before rerunning reactive scopes (use_memo, use_effect, use_resource, components). With that fix, the effect should not be subscribed to the memo until the effect is finished running which avoids the duplicate run
from dioxus.
Memo<T>
is recomputing the value when you read the memo inside the effect.
Why is recomputing the value necessary at that point?
from dioxus.
Memo<T>
is recomputing the value when you read the memo inside the effect.Why is recomputing the value necessary at that point?
Memos use a push/pull based system to try to maintain consistent. They don't recompute until one of these two conditions is met:
- The value is read
- All other rendering is done
let mut count = use_signal(|| 0);
// memorized will run once to get it's initial value
let memorized = use_memo(move || count() * 2);
// memorized is marked as dirty, but doesn't rerun - Lazily reruning lets us skip calculating the memo value when count = 1
count += 1;
// memorized is marked as dirty, but doesn't rerun
count += 1;
// memorized is read so it is rerun - this means we can never observe an out of date value
println!("{memorized}");
// memorized is marked as dirty, but doesn't rerun
count += 1;
// Once the component (and any parent components are done rendering, memorized is recomputed to see if any reactive closures/components need to be rerun)
from dioxus.
Interesting, thank you for elaborating.
What happens if multiple observers read from the memo? I assume the value is recalculated on the first read, and the dirty flag is removed. The second read then gets the value without recalculating?
from dioxus.
Interesting, thank you for elaborating.
What happens if multiple observers read from the memo? I assume the value is recalculated on the first read, and the dirty flag is removed. The second read then gets the value without recalculating?
Yes, exactly. It will rerun at most once for every write to a signal that is subscribes to
from dioxus.
Related Issues (20)
- how to send channel to child component HOT 8
- `use_reactive` inside `use_memo` causes infinite re-renders HOT 2
- `rsx` macro should prevent any children in self-closing elements
- Provide type checking support for HTML attribute values in `rsx` macro
- HTML String into rsx! HOT 1
- Update TailwindCSS Example Project HOT 2
- Add support for attaching event listeners to the global window HOT 2
- Hot reload doesn't turn off when using `--hot-reload=false`
- Assets in Dioxus build are "unpacked" from the original directory
- Help Request: dx bundle tries to create a directory with a too long path HOT 1
- "node is undefined" when using dx serve HOT 1
- Support the new Popover API natively
- MountedData does not properly being updated after rerendering. HOT 3
- Renaming value doesn't change in rsx macro HOT 1
- `_TAILWIND_URL` returns `Failed to add asset: No such file or directory (os error 2)`
- CLI SCSS Support for Manganis
- Tracking: Development Iteration Time
- Manganis CSS Hot Reloading
- Request for `useLayoutEffect` for the web renderer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dioxus.