In my component's render function I have:
(在组件的render函数中,我有:)
render() {
const items = ['EN', 'IT', 'FR', 'GR', 'RU'].map((item) => {
return (<li onClick={this.onItemClick.bind(this, item)} key={item}>{item}</li>);
});
return (
<div>
...
<ul>
{items}
</ul>
...
</div>
);
}
everything renders fine, however when clicking the <li>
element I receive the following error:
(一切正常,但是单击<li>
元素时,出现以下错误:)
Uncaught Error: Invariant Violation: Objects are not valid as a React child (found: object with keys {dispatchConfig, dispatchMarker, nativeEvent, target, currentTarget, type, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, view, detail, screenX, screenY, clientX, clientY, ctrlKey, shiftKey, altKey, metaKey, getModifierState, button, buttons, relatedTarget, pageX, pageY, isDefaultPrevented, isPropagationStopped, _dispatchListeners, _dispatchIDs}).
(未捕获的错误:始终违反:对象作为React子对象无效(找到:带有键{dispatchConfig,dispatchMarker,nativeEvent,target,currentTarget,type,eventPhase,气泡,cancelable,timeStamp,defaultPrevented,isTrusted,视图,详细信息,screenX的对象,screenY,clientX,clientY,ctrlKey,shiftKey,altKey,metaKey,getModifierState,按钮,按钮,relatedTarget,pageX,pageY,isDefaultPrevented,isPropagationStopped,_dispatchListeners,_dispatchIDs})。)
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.(如果您打算渲染孩子的集合,请改用数组或使用React附加组件中的createFragment(object)包装对象。)
Check the render method of Welcome
.(检查Welcome
的渲染方法。)
If I change to this.onItemClick.bind(this, item)
to (e) => onItemClick(e, item)
inside the map function everything works as expected.
(如果我将map函数内的this.onItemClick.bind(this, item)
更改为(e) => onItemClick(e, item)
,一切都会按预期进行。)
If someone could explain what I am doing wrong and explain why do I get this error, would be great
(如果有人可以解释我在做什么错并解释为什么我会收到此错误,那将是很好的)
UPDATE 1:
(更新1:)
onItemClick function is as follows and removing this.setState results in error disappearing.(onItemClick函数如下,删除this.setState导致错误消失。)
onItemClick(e, item) {
this.setState({
lang: item,
});
}
But I cannot remove this line as I need to update state of this component
(但是我无法删除此行,因为我需要更新此组件的状态)
ask by almeynman translate from so