how to implement the new react-redux v6.0.0

hannad rehman picture hannad rehman · Dec 11, 2018 · Viewed 9.6k times · Source

i was trying to migrate react-redux v5.X.X to v6.0.0 and there dosent seem to be any documentation for it. i am using following versions : "react": "^16.4.2" "redux": "^4.0.0" "react-redux": "^6.0.0"

the official change log says.

Passing store as a prop to a connected component is no longer supported. Instead, you may pass a custom context={MyContext} prop to both and . You may also pass {context : MyContext} as an option to connect. link is here

here is my root index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { configureStore, history } from './Store';
import App from './App.hot';

import 'antd/dist/antd.min.css';
const reduxStore = configureStore();
ReactDOM.render(<App store={reduxStore} history={history} />, document.getElementById('root'));

here is my app.jsx (root component)

import React from 'react';
import PropTypes from 'prop-types';
import { Provider, connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { ConnectedRouter } from 'connected-react-router';
import Layout from './Layout';

class App extends React.Component {
  static propTypes = {
    store: PropTypes.object.isRequired,
    history: PropTypes.object.isRequired,
  };

  render() {
    const { store, profile, history } = this.props;
    return (
      <main className="app-wrapper">
        // what i understand from change log is this part 
        // i need to pass context instead of store as props. 
        <Provider store={store}>
          <ConnectedRouter history={history}>
            <Layout user={profile} />
          </ConnectedRouter>
        </Provider>
      </main>
    );
  }
}


function mapStateToProps(store) {
  return {
    ...
  };
}
function mapDispatchToProps(dispatch) {
  return bindActionCreators({
    ...
  }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(App);

as per change log i created context and passed it down to the provider

 const storeContext = React.createContext(reduxStore);

here is my render function after that change

  render() {
    const { store, profile, history } = this.props;
    return (
      <main className="app-wrapper">
        <Provider context={storeContext}>
          <ConnectedRouter history={history}>
            <Layout user={profile} />
          </ConnectedRouter>
        </Provider>
      </main>
    );
  }

passing store as props to provider gives following error

Passing redux store in props has been removed and does not do anything. To use a custom Redux store for specific components, create a custom React context with React.createContext(), and pass the context object to React-Redux's Provider and specific components like: . You may also pass a {context : MyContext} option to connect

and passing as context gives following error

Could not find "store" in the context of "Connect(App)". Either wrap the root component in a , or pass a custom React context provider to and the corresponding React context consumer to Connect(App) in connect options.

i did not find any documentation expect this redux history document here it tells all the problems and solutions for the problem in react-redux and how the context api fixed it. but i am not sure how to actually implement it in real project.

did anyone face the same issue ? or can you please tell me how exactly to implement this change.

thanks

Answer

hannad rehman picture hannad rehman · Dec 25, 2018

I was able to solve the problem by actually listening to what the error message said.

there were two problems with my code

  1. i was passing store as props to my <App /> component. which is why the first warning/error message was comming.

Passing redux store in props has been removed and does not do anything. To use a custom Redux store for specific components, create a custom React context with React.createContext(), and pass the context object to React-Redux's Provider and specific components like: . You may also pass a {context : MyContext} option to connect

to fix this simply dont pass whole redux store as props to any component

  1. my Provider from react-redux was not the root component. the error message said

Could not find "store" in the context of "Connect(App)". Either wrap the root component in a Provider , or pass a custom React context provider to and the corresponding React context consumer to Connect(App) in connect options

so i followed the second wanring in the sentence

Either wrap the root component in a Provider , or pass a custom React context

so i wrapped my main root in provider. and things started working well.

ReactDOM.render(
  <Provider store={reduxStore}>
    <App />
  </Provider>, document.getElementById('root'),
);