我正在使用 MobX @observer 和 @withRouter (react-router-v4) 像这样包装页面组件
@withRouter
@inject('stores')
@observer
class Page extends Component {
render() {
return (
<div>
<NavBar />
<Header title={this.props.stores.UIStore.title} />
<Switch>
<Route exact path="/" component={HomePage} />
<Route exact path="/about" component={AboutPage} />
<Route component={NotFound} />
</Switch>
</div>
)
}
问题
当路由位置更改时,NavBar 和 Header 组件总是使用相同的道具重新渲染(没有任何状态更新)。react-perf 在路由更改时显示许多浪费的渲染(没有道具/状态更新)。
NavBar 包括 Link 和一些 MobX 状态(仅使用 @observer+@inject 包装的 NavBar)
Header 只是一个无状态组件。
页面组件需要 @withRouter 导致 @observer (MobX) 中断 react-router ( https://github.com/mobxjs/mobx-react/issues/210 )
如何防止 NavBar 和 Header 因路由位置更改而重新渲染?只有当 mobx 状态更新时才允许重新渲染。