Open rambip opened 2 months ago
As proposed by @ealmloff , a possible way to solve this issue is to add a way to automatically reset the component when an input changes.
This was discussed some more on the discord. use_branched_signal
looses information when you update the signal manually. It is a bit too complex for dioxus to adopt in the core hooks package
Dioxus does let you reset a component by putting it in an iterator with a key that depends on the props (or hash of the props). React published an article about a very similar issue a while ago: https://legacy.reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key
{std::iter::once(
rsx! { Explorer { key: "{positions:?}-{time_window:?}", link_stream: stream, initial_positions: positions, initial_time_windw: time_window } }
)}
We could provide a wrapper over the iterator approach with a more clear name:
Reset {
when_value_changes: (positions, time_window),
Explorer { link_stream: stream, initial_positions: positions, initial_time_windw: time_window }
}
Related issues/discussions:
/user/123
-> /user/1234
)
Feature Request
Let's say I want to build a web app where the state is defined this way:
Initially, the state is
HomeScreen
, but after the user go through the initial setup, the state isApp{favorite_food: "pasta"}
(let's say the favorite_food comes from a http request). I then want to change the initial "favorite_food" guess if the user changes this information. Conceptually, the code would look like this:The issue is that I don't know how to create a new
Signal
from aReadOnlySignal
. This new signal should update both wheninitial_relative_food
is updated and when the componentApp
changes it (interior mutability)Non-solutions
provide
initial_favorite_food
as a value instead of a signalIt works, but if the
state
signal is updated with a new value, it will not update the component.This solution also won't work for a non-
Clone
value.use
use_effect
As said in the documentation, "Effects are reactive closures that run after the component has finished rendering". It should not be used to derive state, but I want to derive state in this case.
use
use_memo
This would allow to do some computation to
initial_favorite_food
and react to changes, but no interior mutability.Implement Suggestion
I think I found a nice way to implement the behaviour I want:
To use it, simply do
Other uses
In essence, my proposition is just a new hook that is identical to
use_signal
, but in addition it update when a dependency in the closure changes. I think it would allow a lot of other nice patterns that would otherwise be impossible (or require optional values with unwraps)