You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
import React, { useMemo, useEffect } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { ReactReduxContext } from './Context';
|
|
import Subscription from '../utils/Subscription';
|
|
|
|
function Provider(_ref) {
|
|
var store = _ref.store,
|
|
context = _ref.context,
|
|
children = _ref.children;
|
|
var contextValue = useMemo(function () {
|
|
var subscription = new Subscription(store);
|
|
subscription.onStateChange = subscription.notifyNestedSubs;
|
|
return {
|
|
store: store,
|
|
subscription: subscription
|
|
};
|
|
}, [store]);
|
|
var previousState = useMemo(function () {
|
|
return store.getState();
|
|
}, [store]);
|
|
useEffect(function () {
|
|
var subscription = contextValue.subscription;
|
|
subscription.trySubscribe();
|
|
|
|
if (previousState !== store.getState()) {
|
|
subscription.notifyNestedSubs();
|
|
}
|
|
|
|
return function () {
|
|
subscription.tryUnsubscribe();
|
|
subscription.onStateChange = null;
|
|
};
|
|
}, [contextValue, previousState]);
|
|
var Context = context || ReactReduxContext;
|
|
return React.createElement(Context.Provider, {
|
|
value: contextValue
|
|
}, children);
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Provider.propTypes = {
|
|
store: PropTypes.shape({
|
|
subscribe: PropTypes.func.isRequired,
|
|
dispatch: PropTypes.func.isRequired,
|
|
getState: PropTypes.func.isRequired
|
|
}),
|
|
context: PropTypes.object,
|
|
children: PropTypes.any
|
|
};
|
|
}
|
|
|
|
export default Provider; |