UNPKG

custom-app

Version:

ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������

77 lines (59 loc) 1.78 kB
import React, { Component } from 'react' import PropTypes from 'prop-types' import { ReactReduxContext } from './Context' import Subscription from '../utils/Subscription' class Provider extends Component { constructor(props) { super(props) const { store } = props this.notifySubscribers = this.notifySubscribers.bind(this) const subscription = new Subscription(store) subscription.onStateChange = this.notifySubscribers this.state = { store, subscription } this.previousState = store.getState() } componentDidMount() { this._isMounted = true this.state.subscription.trySubscribe() if (this.previousState !== this.props.store.getState()) { this.state.subscription.notifyNestedSubs() } } componentWillUnmount() { if (this.unsubscribe) this.unsubscribe() this.state.subscription.tryUnsubscribe() this._isMounted = false } componentDidUpdate(prevProps) { if (this.props.store !== prevProps.store) { this.state.subscription.tryUnsubscribe() const subscription = new Subscription(this.props.store) subscription.onStateChange = this.notifySubscribers this.setState({ store: this.props.store, subscription }) } } notifySubscribers() { this.state.subscription.notifyNestedSubs() } render() { const Context = this.props.context || ReactReduxContext return ( <Context.Provider value={this.state}> {this.props.children} </Context.Provider> ) } } 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