Commit 87828691 authored by Robert Knight's avatar Robert Knight

Document a hazard with `useStore` and hook that have dependencies

Note a hazard with using the store wrapper returned by `useStore` with
hooks that have dependencies, such as `useMemo`. I believe it should be
possible to eliminate this hazard with some API changes, but this will
require some experimentation. For the moment, document the issue and a
recommended workaround.
parent 901e95bb
...@@ -38,6 +38,24 @@ class CacheEntry { ...@@ -38,6 +38,24 @@ class CacheEntry {
* *
* The returned wrapper has the same API as the store itself. * The returned wrapper has the same API as the store itself.
* *
* The returned wrapper does not change its identity if the store updates. This
* means you need to be careful when using it with hooks that have dependencies,
* such as `useMemo`, `useEffect` or `useCallback`. Given code like this:
*
* ```
* const calculatedValue = useMemo(() => calculateSomething(store.getSomeValue()), [store]);
* ```
*
* `calulatedValue` will not be recalculated if the result of
* `store.getSomeValue()` changes, because the `store` reference itself does not
* change. A workaround is to extract the values from the store and pass those
* into the closure:
*
* ```
* const someValue = store.getSomeValue();
* const calculatedValue = useMemo(() => calculateSomething(someValue), [someValue]);
* ```
*
* @example * @example
* // A hook which encapsulates looking up the specific store instance, * // A hook which encapsulates looking up the specific store instance,
* // eg. via `useContext`. * // eg. via `useContext`.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment