Reactjs 将逻辑移出componentDidUpdate的提示
我有一个redux连接的组件,我正在使用Reactjs 将逻辑移出componentDidUpdate的提示,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我有一个redux连接的组件,我正在使用componentdiddupdate进行大量逻辑操作,它开始变得混乱起来 我的componentdiddupdate看起来有点像这样: componentDidUpdate(prevProps: Props) { if (this.props === prevProps) { return; } const { history, a, b, c } = th
componentdiddupdate
进行大量逻辑操作,它开始变得混乱起来
我的componentdiddupdate
看起来有点像这样:
componentDidUpdate(prevProps: Props) {
if (this.props === prevProps) {
return;
}
const {
history,
a,
b,
c
} = this.props;
if (!prevProps.a && !a) {
this.proceedToNextStep();
return;
}
if (b.length === b.length + 1) {
history.push(rootUrl);
return;
}
if (c != prevProps.c) {
// do stuff
return;
}
}
由于redux操作,道具将发生变化,但我不知道有什么更好的地方可以确定redux连接组件中发生了什么变化
对于一个属性来说,这可能是错误的,但是这里出现了更多的逻辑,这已经很混乱了。您可以将逻辑放入:
- :这至少可以避免在执行组件逻辑之前重新呈现组件。新道具将包含Redux
connect注入的道具
- :Redux
第三个参数是一个回调,由合并的props发送:(state props+dispatch props+own component props)connect
- 包装:这对我来说是处理逻辑的最佳选择,因为它不涉及React组件:我可以链接js逻辑(带有可观察对象),并且在逻辑执行的每个步骤中,我可以触发
并访问应用程序Reduxdispatch()
。模块混合了模块(可观察)和模块(redux promise)以便于使用状态