Difference between component and container in react redux

前端 未结 7 1934
青春惊慌失措
青春惊慌失措 2020-12-22 18:27

What is the difference between component and container in react redux?

相关标签:
7条回答
  • 2020-12-22 19:09

    The components construct a piace of the view, so it should render DOM elements, put content on the screen.

    The containers participate in the data elaboration, so it should "talk" with redux, because it will need to modify the state. So, you should include connect (react-redux) what it makes the connection, and the functions mapStateToProps and mapDispatchToProps :

    .
    .
    .
    import { connect } from "react-redux";
    
    class myContainer extends Component {
    }
    
    function mapStateToProps(state) {
      // You return what it will show up as props of myContainer
      return {
        property: this.state.property
      };
    }
    
    function mapDispatchToProps(dispatch) {
      // Whenever property is called, it should be passed to all reducers
      return bindActionCreators({ property: property }, dispatch);
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(myContainer);
    
    0 讨论(0)
提交回复
热议问题