react-redux#ReactReduxContext JavaScript Examples

The following examples show how to use react-redux#ReactReduxContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: injectSaga.js    From QiskitFlow with Apache License 2.0 6 votes vote down vote up
useInjectSaga = ({ key, saga, mode }) => {
  const context = React.useContext(ReactReduxContext);
  React.useEffect(() => {
    const injectors = getInjectors(context.store);
    injectors.injectSaga(key, { saga, mode });

    return () => {
      injectors.ejectSaga(key);
    };
  }, []);
}
Example #2
Source File: index.js    From flatris-LAB_V1 with MIT License 6 votes vote down vote up
render() {
    return (
      <Layout>
        <ReactReduxContext.Consumer>
          {({ store }) => (
            <SocketProvider store={store}>
              <Dashboard />
            </SocketProvider>
          )}
        </ReactReduxContext.Consumer>
      </Layout>
    );
  }
Example #3
Source File: join.js    From flatris-LAB_V1 with MIT License 6 votes vote down vote up
render() {
    const { statusCode } = this.props;
    if (statusCode) {
      return (
        <Layout>
          <Error statusCode={statusCode} />
        </Layout>
      );
    }

    return (
      <Layout>
        <Title>Play Flatris</Title>
        <ReactReduxContext.Consumer>
          {({ store }) => (
            <SocketProvider store={store}>
              <CurGameOfElse else={() => Router.replace('/')}>
                <FlatrisGame />
              </CurGameOfElse>
            </SocketProvider>
          )}
        </ReactReduxContext.Consumer>
      </Layout>
    );
  }
Example #4
Source File: new.js    From flatris-LAB_V1 with MIT License 6 votes vote down vote up
render() {
    return (
      <Layout>
        <ReactReduxContext.Consumer>
          {({ store }) => (
            <SocketProvider store={store}>
              <NewGame />
            </SocketProvider>
          )}
        </ReactReduxContext.Consumer>
      </Layout>
    );
  }
Example #5
Source File: injectSaga.js    From hackchat-client with Do What The F*ck You Want To Public License 6 votes vote down vote up
useInjectSaga = ({ key, saga }) => {
  const context = React.useContext(ReactReduxContext);
  React.useEffect(() => {
    const injectors = getInjectors(context.store);
    injectors.injectSaga(key, { saga });

    return () => {
      injectors.ejectSaga(key);
    };
  }, []);
}
Example #6
Source File: injectReducer.js    From QiskitFlow with Apache License 2.0 5 votes vote down vote up
useInjectReducer = ({ key, reducer }) => {
  const context = React.useContext(ReactReduxContext);
  React.useEffect(() => {
    getInjectors(context.store).injectReducer(key, reducer);
  }, []);
}
Example #7
Source File: injectReducer.js    From hackchat-client with Do What The F*ck You Want To Public License 5 votes vote down vote up
useInjectReducer = ({ key, reducer }) => {
  const context = React.useContext(ReactReduxContext);
  React.useEffect(() => {
    getInjectors(context.store).injectReducer(key, reducer);
  }, []);
}
Example #8
Source File: createDevTools.js    From Learning-Redux with MIT License 4 votes vote down vote up
export default function createDevTools(children) {
  const monitorElement = Children.only(children);
  const monitorProps = monitorElement.props;
  const Monitor = monitorElement.type;
  const ConnectedMonitor = connect(state => state)(Monitor);

  return class DevTools extends Component {
    static contextTypes = {
      store: PropTypes.object
    };

    static propTypes = {
      store: PropTypes.object
    };

    static instrument = (options) => instrument(
      (state, action) => Monitor.update(monitorProps, state, action),
      options
    );

    constructor(props, context) {
      super(props, context);

      if (ReactReduxContext) {
        if (this.props.store && !this.props.store.liftedStore) {
          logError('NoLiftedStore');
        }
        return;
      }

      if (!props.store && !context.store) {
        logError('NoStore');
        return;
      }

      if (context.store) {
        this.liftedStore = context.store.liftedStore;
      } else {
        this.liftedStore = props.store.liftedStore;
      }

      if (!this.liftedStore) {
        logError('NoLiftedStore');
      }
    }

    render() {
      if (ReactReduxContext) {
        // For react-redux@6
        if (this.props.store) {
          if (!this.props.store.liftedStore) {
            return null;
          }
          return (
            <Provider store={this.props.store.liftedStore}>
              <ConnectedMonitor {...monitorProps} />
            </Provider>
          );
        }
        return (
          <ReactReduxContext.Consumer>
          {props => {
            if (!props || !props.store) {
              logError('NoStore');
              return null;
            }
            if (!props.store.liftedStore) {
              logError('NoLiftedStore');
              return null;
            }
            return (
              <Provider store={props.store.liftedStore}>
                <ConnectedMonitor {...monitorProps} />
              </Provider>
            );
          }}
        </ReactReduxContext.Consumer>
        );
      }

      if (!this.liftedStore) {
        return null;
      }

      return (
        <ConnectedMonitor {...monitorProps} store={this.liftedStore} />
      );
    }
  };
}
Example #9
Source File: ConnectedRouter.js    From the-eye-knows-the-garbage with MIT License 4 votes vote down vote up
createConnectedRouter = function createConnectedRouter(structure) {
  var _createSelectors = createSelectors(structure),
      getLocation = _createSelectors.getLocation;
  /*
   * ConnectedRouter listens to a history object passed from props.
   * When history is changed, it dispatches action to redux store.
   * Then, store will pass props to component to render.
   * This creates uni-directional flow from history->store->router->components.
   */


  var ConnectedRouter =
  /*#__PURE__*/
  function (_PureComponent) {
    _inherits(ConnectedRouter, _PureComponent);

    function ConnectedRouter(props) {
      var _this;

      _classCallCheck(this, ConnectedRouter);

      _this = _possibleConstructorReturn(this, _getPrototypeOf(ConnectedRouter).call(this, props));
      var store = props.store,
          history = props.history,
          onLocationChanged = props.onLocationChanged;
      _this.inTimeTravelling = false; // Subscribe to store changes to check if we are in time travelling

      _this.unsubscribe = store.subscribe(function () {
        // Extract store's location
        var _getLocation = getLocation(store.getState()),
            pathnameInStore = _getLocation.pathname,
            searchInStore = _getLocation.search,
            hashInStore = _getLocation.hash; // Extract history's location


        var _history$location = history.location,
            pathnameInHistory = _history$location.pathname,
            searchInHistory = _history$location.search,
            hashInHistory = _history$location.hash; // If we do time travelling, the location in store is changed but location in history is not changed

        if (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore) {
          _this.inTimeTravelling = true; // Update history's location to match store's location

          history.push({
            pathname: pathnameInStore,
            search: searchInStore,
            hash: hashInStore
          });
        }
      });

      var handleLocationChange = function handleLocationChange(location, action) {
        var isFirstRendering = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

        // Dispatch onLocationChanged except when we're in time travelling
        if (!_this.inTimeTravelling) {
          onLocationChanged(location, action, isFirstRendering);
        } else {
          _this.inTimeTravelling = false;
        }
      }; // Listen to history changes


      _this.unlisten = history.listen(handleLocationChange); // Dispatch a location change action for the initial location.
      // This makes it backward-compatible with react-router-redux.
      // But, we add `isFirstRendering` to `true` to prevent double-rendering.

      handleLocationChange(history.location, history.action, true);
      return _this;
    }

    _createClass(ConnectedRouter, [{
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        this.unlisten();
        this.unsubscribe();
      }
    }, {
      key: "render",
      value: function render() {
        var _this$props = this.props,
            history = _this$props.history,
            children = _this$props.children;
        return React.createElement(Router, {
          history: history
        }, children);
      }
    }]);

    return ConnectedRouter;
  }(PureComponent);

  ConnectedRouter.propTypes = {
    store: PropTypes.shape({
      getState: PropTypes.func.isRequired,
      subscribe: PropTypes.func.isRequired
    }).isRequired,
    history: PropTypes.shape({
      action: PropTypes.string.isRequired,
      listen: PropTypes.func.isRequired,
      location: PropTypes.object.isRequired,
      push: PropTypes.func.isRequired
    }).isRequired,
    basename: PropTypes.string,
    children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
    onLocationChanged: PropTypes.func.isRequired
  };

  var mapDispatchToProps = function mapDispatchToProps(dispatch) {
    return {
      onLocationChanged: function onLocationChanged(location, action, isFirstRendering) {
        return dispatch(_onLocationChanged(location, action, isFirstRendering));
      }
    };
  };

  var ConnectedRouterWithContext = function ConnectedRouterWithContext(props) {
    var Context = props.context || ReactReduxContext;

    if (Context == null) {
      throw 'Please upgrade to react-redux v6';
    }

    return React.createElement(Context.Consumer, null, function (_ref) {
      var store = _ref.store;
      return React.createElement(ConnectedRouter, _extends({
        store: store
      }, props));
    });
  };

  ConnectedRouterWithContext.propTypes = {
    context: PropTypes.object
  };
  return connect(null, mapDispatchToProps)(ConnectedRouterWithContext);
}