docs: strict null check in createContext example (#2995)

* docs: prefer useState in createContext example

Both useState and useRef are correct in practice: useState will only render once since we never set the state after initializion, and useRef is safe to use since the component never needs to rerender when the ref value changes.

However, lint rules will flag useRef as a mistake, because we are passing a ref value to a child component, which in general can cause stale components since changing refs doesn't trigger re-renders.

* formatting

* explicit null check
This commit is contained in:
jroitgrund 2025-03-20 13:23:42 +01:00 committed by GitHub
parent 6c7699cc16
commit 17e281fd75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -118,7 +118,7 @@ const StoreContext = createContext(null)
const StoreProvider = ({ children }) => {
const storeRef = useRef()
if (!storeRef.current) {
if (storeRef.current === null) {
storeRef.current = createStore((set) => ({
// ...
}))