MCPcopy
hub / github.com/facebook/react / StoreProvider

Function StoreProvider

compiler/apps/playground/components/StoreContext.tsx:35–64  ·  view source on GitHub ↗
({children}: {children: ReactNode})

Source from the content-addressed store, hash-verified

33 * Make Store and dispatch function available to all sub-components in children.
34 */
35export function StoreProvider({children}: {children: ReactNode}): JSX.Element {
36 const [store, dispatch] = useReducer(storeReducer, emptyStore);
37 const [isPageReady, setIsPageReady] = useState<boolean>(false);
38
39 useEffect(() => {
40 let mountStore: Store;
41 try {
42 mountStore = initStoreFromUrlOrLocalStorage();
43 } catch (e) {
44 console.error('Failed to initialize store from URL or local storage', e);
45 mountStore = defaultStore;
46 }
47 dispatch({type: 'setStore', payload: {store: mountStore}});
48 setIsPageReady(true);
49 }, []);
50
51 useEffect(() => {
52 if (store !== emptyStore) {
53 saveStore(store);
54 }
55 }, [store]);
56
57 return (
58 <StoreContext.Provider value={store}>
59 <StoreDispatchContext.Provider value={dispatch}>
60 {isPageReady ? children : null}
61 </StoreDispatchContext.Provider>
62 </StoreContext.Provider>
63 );
64}
65
66type ReducerAction =
67 | {

Callers

nothing calls this directly

Calls 7

useReducerFunction · 0.90
useStateFunction · 0.90
useEffectFunction · 0.90
saveStoreFunction · 0.90
errorMethod · 0.65
dispatchFunction · 0.50

Tested by

no test coverage detected