How to use connect from react redux?

Technology CommunityCategory: ReduxHow to use connect from react redux?
VietMX Staff asked 3 years ago

You need to follow two steps to use your store in your container 1. Use mapStateToProps(): It maps the state variables from your store to the props that you specify 2. Connect the above props to your container: The object returned by the mapStateToProps component is connected to the container. You can import connect from react-redux like

import React from 'react';
import { connect } from 'react-redux';

 class App extends React.Component {
   render() {
     return <div>{this.props.containerData}</div>;
   }
 }

 function mapStateToProps(state) {
   return { containerData: state.appData };
 }

 export default connect(mapStateToProps)(App);

function mapStateToProps(state) {
  return { containerData: state.data };
}

export default connect(mapStateToProps)(App);