[hooks] Allow changing cursor path dynamically #135
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With the current implementation, when
cursors
changes, a new watcheris created and we subscribe to "update" event. The issue is, if the
value under new path never changes,
useBranch
will always return thevalue from the previous path.
In example below (from documentation), changing
alternative
propwill not change
colors
after first render.Fix this by re-fetching the current value in the
useEffect
.Another issue is that with the current examples,
selectors
changeson every render. Passing in an object or function inline will create a
new instance of object on every render, triggering watcher
re-creation.
With the fix above, that leads to an infinite loop:
useEffect
sets new value forstate
state
triggers a renderselectors
triggeringuseEffect
Fix this by adding a
deps
argument that should hold an array ofdependencies to trigger re-subscription. The default value is now an
empty array to break the infinite loop and be more
backward-compatible.
Another alternative is to use
useMemo
to memoize selectors inexamples, but that is overly-verbose and would require a lot of
migration effort for users.