Reactjs 获取未捕获错误:对象作为“React-child”错误无效,我不知道原因
因此,我正在尝试做的,并且一直没有遇到问题,就是将一个函数传递给子组件,子组件返回一个值,最后,返回的值被设置在父组件中父组件的状态中。它看起来像这样:Reactjs 获取未捕获错误:对象作为“React-child”错误无效,我不知道原因,reactjs,Reactjs,因此,我正在尝试做的,并且一直没有遇到问题,就是将一个函数传递给子组件,子组件返回一个值,最后,返回的值被设置在父组件中父组件的状态中。它看起来像这样: export default class Parent extends React.Component { constructor(props){ super(props); this.state = {active: false, currId: ""}; }; setStateFromChild(val, i
export default class Parent extends React.Component {
constructor(props){
super(props);
this.state = {active: false, currId: ""};
};
setStateFromChild(val, id){
this.setState({active: val, currId: id});
}
renderChildren() {
return this.props.data.map( (item) =>
<ChildComponent returnState={this.setStateFromChild.bind(this)} active={this.state.currId === item.id ? true : false} id={item.id}/>
)
}
render(){
return(<div>{this.renderChildren()}</div>)
}
}
然后子组件
export default class ChildComponent extends React.Component {
constructor(props){
super(props);
this.state = {active: props.active}
};
componentWillReceiveProps(nextProps){
if(nextProps.active !== this.state.active){
this.setState({active: nextProps.active});
}
}
handleChange(e){
e.preventDefault();
this.props.returnState(!this.state.active, this.props.id);
}
render(){
return(<button className={this.state.active ? "green button" : "grey button"} onClick={this.handleChange.bind(this)}>Select Option</button>)
}
}
然后我得到以下错误:
未捕获错误:对象作为找到的子对象无效:对象
使用键{key,val}。如果要呈现
子对象,改为使用数组或使用
从React加载项创建FragmentObject
我在任何地方都看不到该对象是渲染的React子对象。我唯一拥有的对象是父组件中的状态更改本身。我做了多次,在这一特定组件出现之前从未遇到过问题。非常感谢您的帮助
编辑:我还必须补充一点,设置状态是有效的,但是这个错误会一直在控制台中打印并减慢我的应用程序速度。我不知道错误是从哪里来的。不确定。但是,renderChildren不会返回任何内容。这不确定。但是,renderChildren不会返回任何内容。这是有效的