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.
useStore Implementation Notes
This is a prototype implementation of a
createStorefunction anduseStorehook for React, which allows components to subscribe to external data stores.It is based on our previous exploration of a
use(store)API as well as exploration done in thereact-concurrent-storerepository.Architecture
Each React root maintains a StoreTracker which is a reference-counted registry of all stores used within that root. For each store, a StoreWrapper is created which tracks the committed and transition state(s) for that store within that root.
The wrapper is also responsible for tracking fibers which are subscribed to the store, and scheduling updates to those fibers when the store changes.
For the sake of expediency, the useStore hook is currently implemented in terms of the existing useReducer implementation. While this forces us to use some awkward hacks it has the advantage of keeping the implementation minimally invasive for this initial prototype/validation phase. For example, we're able to reuse all the existing hook queue/update logic without modifications, which would leak out into the rest of React.
Hacks
dispatchfield of the hook queue to store the arguments to the useStore hook (the store wrapper and selector). This allows us to access them from within the updateStore function, which is called outside the render context.