示例代码:https://github.com/d6u/example-redux-update-nested-props/blob/master/one-connect/index.js
观看现场演示:http://d6u.github.io/example-redux-update-nested-props/one-connect.html
How to optimize small updates to props of nested component?
我有上述组件,回购和回购 list .我想更新第一次回购的标签(Line 14).所以我派出了UPDATE_TAG
行动.在我实现shouldComponentUpdate
之前,调度大约需要200毫秒,这是预期的,因为我们正在浪费大量时间来区分没有改变的<Repo/>
秒.
添加shouldComponentUpdate
后,调度大约需要30毫秒.在生产完成后,我们会做出react .js,更新只需17毫秒.这要好得多,但Chrome开发控制台中的时间线视图仍然显示jank frame(超过16.6ms).
想象一下,如果我们有很多这样的更新,或者<Repo/>
比当前的更新更复杂,我们将无法维持60fps.
My question is, for such small updates to a nested component's props, is there a more efficient and canonical way to update the content? Can I still use Redux?
我找到了一个解决方案,用一个可观察的内部减速器替换每tags
个.差不多
// inside reducer when handling UPDATE_TAG action
// repos[0].tags of state is already replaced with a Rx.BehaviorSubject
get('repos[0].tags', state).onNext([{
id: 213,
text: 'Node.js'
}]);
然后我用https://github.com/jayphelps/react-observable-subscribe在回购组件中订阅它们的值.这很有效.即使开发了React,每次调度也只需5毫秒.js.但我觉得这是Redux的反模式.
更新1
我遵循了丹·阿布拉莫夫的答案和normalized my state和updated connect components中的建议
新的州形态是:
{
repoIds: ['1', '2', '3', ...],
reposById: {
'1': {...},
'2': {...}
}
}
我在时间the initial rendering的基础上加了console.time
.
但是,性能比以前差(包括初始渲染和更新).(来源:https://github.com/d6u/example-redux-update-nested-props/blob/master/repo-connect/index.js,现场演示:http://d6u.github.io/example-redux-update-nested-props/repo-connect.html)
// With dev build
INITIAL: 520.208ms
DISPATCH: 40.782ms
// With prod build
INITIAL: 138.872ms
DISPATCH: 23.054ms
我认为每<Repo/>
个连接都有很多开销.
更新2
根据Dan的更新答案,我们必须返回connect
的mapStateToProps
个参数,而不是返回一个函数.你可以看看丹的答案.我还更新了the demos条.
下面,我的电脑的性能要好得多.为了好玩,我还添加了我所说的方法(sourcedemo)(seriously don't use it, it's for experiment only)中的副作用.
// in prod build (not average, very small sample)
// one connect at root
INITIAL: 83.789ms
DISPATCH: 17.332ms
// connect at every <Repo/>
INITIAL: 126.557ms
DISPATCH: 22.573ms
// connect at every <Repo/> with memorization
INITIAL: 125.115ms
DISPATCH: 9.784ms
// observables + side effect in reducers (don't use!)
INITIAL: 163.923ms
DISPATCH: 4.383ms
更新3
刚刚根据"每一次都与记忆联系"增加了react-virtualized example个
INITIAL: 31.878ms
DISPATCH: 4.549ms