Javascript 子组件赢得';t在没有函数属性的情况下更新

Javascript 子组件赢得';t在没有函数属性的情况下更新,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我的React组件出现了一种非常奇怪的行为,我无法解释 这是我的应用程序的缩小版本: 类子级扩展React.Component{ 组件更新(上一个){ 如果(!prev.isActive&&this.props.isActive){ console.log('activated',this.props.title); } } render(){ 返回( {this.props.title} ); } } const Child1=reactRedux.connect((状态、道具)=>({ i

我的React组件出现了一种非常奇怪的行为,我无法解释

这是我的应用程序的缩小版本:

类子级扩展React.Component{
组件更新(上一个){
如果(!prev.isActive&&this.props.isActive){
console.log('activated',this.props.title);
}
}
render(){
返回(
{this.props.title}
);
}
}
const Child1=reactRedux.connect((状态、道具)=>({
isActive={props.isActive('Child1')}
onClick={()=>props.activate('Child1')}
title=“Child1”
}))(儿童);
const Child2=reactRedux.connect((状态、道具)=>({
isActive={props.isActive('Child2')}
onClick={()=>props.activate('Child2')}
title=“Child2”
}))(儿童);
类根扩展了React.Component{
状态={active:null};
激活=(激活)=>{
如果(!this.isActive(active)){
this.setState({active});
}
};
isActive=active=>this.state.active===active;
renderChild=(ChildComponent,i)=>(
);
render(){
const children=[Child1,Child2];
返回(
{children.map(this.renderChild)}
);
}
}
const reducer=(状态、动作)=>({});
const store=redux.createStore(reducer);
ReactDOM.render(,document.getElementById('root'))

我觉得你的代码很好(除了你没有在
类中使用
构造函数

类子级扩展React.Component{
组件更新(上一个){
如果(!prev.isActive&&this.props.isActive){
console.log('activated',this.props.children);
}
}
render(){
返回(
{this.props.children}
);
}
}
const Child1=道具=>(
props.activate('Child1')}
>
孩子1
);
const Child2=道具=>(
props.activate('Child2')}
>
孩子2
);
类根扩展了React.Component{
状态={active:null};
激活=(激活)=>{
如果(!this.isActive(active)){
this.setState({active});
}
};
isActive=active=>this.state.active===active;
renderChild=(ChildComponent,i)=>(
);
render(){
const children=[Child1,Child2];
返回{children.map(this.renderChild)};
}
}
ReactDOM.render(,document.getElementById('root'))

好的,我找到了一个解决方案(和一个原因)。此行为来自
react redux
性能优化。我必须告诉react redux对其进行优化,它将起作用。这个问题有点相关:


您是否打算调用
componentDidMount
而不是
componentdiddupdate
?顺便问一下,您在哪里调用
activate
?对不起,在本例中我忘记调用activate方法。更新了我的问题。当我单击跨距时,根组件的状态将正确更新,但是子组件的componentDidUpdate方法不会更新。重新更新后,实际代码是正确的,这只是这里的缩小示例,有以下错误:/这似乎是react redux的一个问题。Child1和Child2是react redux连接的组件。我将更新我的示例。尝试包含一个正在运行的代码段:)他不需要构造函数,这在他的原型链中,还使用静态和成员属性babel transform,允许他直接执行
状态={}
,以及通过
=>
将方法绑定到实例,我知道他可以使用babel transform执行该语法,这就是为什么我没动他的密码。不过我确实提到了它,因为它被认为是react中的一种模式,用于将初始代码放入构造函数中。它被认为是OOP中的一种模式,用于将[x]_代码放入您希望重写/扩展的方法中。他没有,尽管中间件为他做了。对于构造函数中的代码,您还可以执行类似于
shouldComponentUpdate(…args){return(localLogic)&&super.shouldComponentUpdate(…args)}
的操作。