每次调用mapStateToProps时,withRouter都会创建一个匹配的新对象

时间:2018-05-09 17:34:34

标签: reactjs react-router react-router-dom react-router-redux

不确定其他人是否在此之前遇到此问题:

我正在使用:     " react-redux":" 5.0.7",     " react-router":" 4.2.0",     " react-router-dom":" 4.2.2"

所以我有一个像这样的组件,

shouldComponentUpdate = (nextProps) => {
    // shallow compare each prop and see which one is changed
    console.log(nextProps.history === this.props.history) // true, good
    console.log(nextProps.location === this.props.location) // true, good
    console.log(nextProps.match === this.props.match) // false, what? althought the value inside match is the same? why match is a new object/reference every time?
}

const mapStateToProps = (state, ownProps) => {
  nothing special here, just merge state and ownProps. (Router location/match/history is in ownProps, withRouter inject them in i think)
  at the end of this function i just return the merge of {state, ownProps}
  same issue exists even without my own mapStateToProps
}

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MyComponent))

每次状态更改时,都会调用mapStateToProps。 withRouter注入位置/匹配/历史进入ownProps。但出于某种原因,匹配'每次调用mapStateToProps时都是一个新对象,因此它会导致不正常的重新渲染。

0 个答案:

没有答案