React Redux - Why mapStateToProps is called before constructor?

前端 未结 1 1274
臣服心动
臣服心动 2021-02-08 19:13

Two questions:

  1. Why mapStateToProps is called before constructor?
  2. As a side-effect of 1

    constructor (props) { base(props

相关标签:
1条回答
  • 2021-02-08 20:03

    mapStateToProps is not called magically before your constructor. It is done by connect which is a Higher Order Component that executes mapStateToProps before your component is initialised. In fact, connect initialises your component in its body.

    connect(mapStateToProps, mapDispatchToProps)(YourComponent)
    

    Why componentWillReceiveProps not executed? Because React doesn't call componentWillReceiveProps for the initial render, so you should use componentDidMount instead.

    componentWillReceiveProps

    Invoked when a component is receiving new props. This method is not called for the initial render.

    0 讨论(0)
提交回复
热议问题