Javascript componentDidUpdate比react redux落后两步
所以我有一个react-redux应用程序,用户在其中输入,然后将输入发送到存储,然后显示在其他组件中。当我在reducer中更新输入数组时,它工作得非常好,但现在我在componentDidUpdate生命周期方法中更新了它,它比预期结果落后了两步,而且如果我输入与以前相同的内容,它也不会记录。这就是代码Javascript componentDidUpdate比react redux落后两步,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,所以我有一个react-redux应用程序,用户在其中输入,然后将输入发送到存储,然后显示在其他组件中。当我在reducer中更新输入数组时,它工作得非常好,但现在我在componentDidUpdate生命周期方法中更新了它,它比预期结果落后了两步,而且如果我输入与以前相同的内容,它也不会记录。这就是代码 class Equations extends React.Component{ componentDidUpdate(nextProps){ console.l
class Equations extends React.Component{
componentDidUpdate(nextProps){
console.log(nextProps.equation)
this.props.equations.unshift(nextProps.equation)
}
render(){
return (
<div>
{this.props.equations.map((val,index)=>{
return (
<div className={index} key={index}>
<p>{val}</p>
</div>
)
})}
</div>
)
}
}
const mapStateToProps = state => ({
equations: state.ER.equations,
equation: state.ER.equation
})
export default connect(mapStateToProps, null)(Equations)
类表达式扩展了React.Component{
组件更新(下一步){
console.log(nextrops.equation)
this.props.equations.unshift(nextrops.equation)
}
render(){
返回(
{this.props.equations.map((val,index)=>{
返回(
{val}
)
})}
)
}
}
常量mapStateToProps=状态=>({
方程:状态方程,
方程:状态方程
})
导出默认连接(mapStateToProps,null)(表达式)
这是日志
上次我输入了foo3-bar3,但直到我输入foo4-bar4它才被记录下来
在我的应用程序中,这就是我使用组件的方式
class App extends React.Component{
render(){
return (
<Provider store={store}>
<div>
<Calculator />
<Equations />
</div>
</Provider>
)
}
}
类应用程序扩展了React.Component{
render(){
返回(
)
}
}
你不应该使用unshift来改变道具。这是错误的做法吗?我不应该使用componentDidUpdate?是的,这是错误的做法,道具来自redux商店状态,你正在改变它,所以你正在改变商店。我现在很害怕看到您的还原程序。componentDidupdate将在每次调度的操作上调用,因为您的mapStateToProps总是返回一个新的对象引用。不知道为什么不在mapStateToProps中提供正确的道具结构,因为这就是它的用途。