父类有几个子组件,所有的子都是在render方法内部实例化的,所以任何对父状态的改变都会导致所有的子实例都被重新实例化,每次渲染都被调用react创建新实例儿童,由失去孩子的状态,以重用子实例我试图使用检索孩子parent.refs.childRef例如有,但是这给了我错误在反应渲染中重用组件实例
Uncaught Error: Objects are not valid as a React child
,这里是我的代码
placeHolderComponent(){
let component;
let palceHolderValue=this.state.uiState.placeHolder;
let classInstance=this;
if(palceHolderValue=='empty'){
component=<EmptyComponent></EmptyComponent>
}
else if(palceHolderValue=='search'){
component= classInstance.refs.gpSearchComponent!=null? classInstance.refs.gpSearchComponent: <GpSearch ref="gpSearchComponent"/>
}
return component;
}
此处GpSearch组件用ref属性实例化,代码检查parent.refs.childComponentRefId是否为null,然后渲染该实例,而不是新实例。我收到此错误
Uncaught Error: Objects are not valid as a React child (.... If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons