import React, { useMemo } from 'react'; import { ReactReduxContext } from './Context'; import { createSubscription } from '../utils/Subscription'; import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'; function Provider({ store, context, children, serverState }) { const contextValue = useMemo(() => { const subscription = createSubscription(store); return { store, subscription, getServerState: serverState ? () => serverState : undefined }; }, [store, serverState]); const previousState = useMemo(() => store.getState(), [store]); useIsomorphicLayoutEffect(() => { const { subscription } = contextValue; subscription.onStateChange = subscription.notifyNestedSubs; subscription.trySubscribe(); if (previousState !== store.getState()) { subscription.notifyNestedSubs(); } return () => { subscription.tryUnsubscribe(); subscription.onStateChange = undefined; }; }, [contextValue, previousState]); const Context = context || ReactReduxContext; // @ts-ignore 'AnyAction' is assignable to the constraint of type 'A', but 'A' could be instantiated with a different subtype return /*#__PURE__*/React.createElement(Context.Provider, { value: contextValue }, children); } export default Provider;