reactjs - 每次调用 mapStateToProps 时,与Router 反应都会为匹配创建一个新对象
问题描述
不确定其他人之前是否遇到过这个问题:
我正在使用:“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 将 location/match/history 注入到 ownProps 中。但是由于某种原因,每次调用 mapStateToProps 时,“匹配”都是一个新对象,因此会导致不必要的重新渲染。
解决方案
推荐阅读
- css - 使用数据库中的数据制作可滚动的引导表
- php - Laravel 将数据库整数转换为字符串
- java - 如何在 install4j 中禁用箭头屏幕导航
- android - Angular 4 逗号不适用于平板电脑 android 键盘
- firefox - svg.node.getScreenCTM() 在 Firefox 中不可用
- amazon-web-services - 修改表后从 AWS AURORA 触发 AWS lambda
- ionic-framework - ion-footer 中的 ion-fab 总是在 ion-content 下
- css - 带有边缘 css hack 的媒体查询
- java - C++/CLI 生成的 HMACSHA256 Hash key 与 Java Generated Hash Key 不同
- git - 重写 git 历史以展平合并提交