在我组件的渲染函数中,我有:

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>
    );
  }

一切呈现良好,但当点击<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}). 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. Check the render method of Welcome.

如果我改成this。onitemclick。绑定(this, item) to (e) => onItemClick(e, item)内的映射函数,一切都按预期工作。

如果有人能解释我做错了什么,为什么我会得到这个错误,那就太好了

更新1: onItemClick函数如下所示。setState会导致错误消失。

onItemClick(e, item) {
    this.setState({
      lang: item,
    });
}

但是我不能删除这一行,因为我需要更新这个组件的状态


当前回答

当您尝试将对象呈现为子元素时,可能会发生这种情况,您可以通过查找下面的场景找到根本原因。

Check if you are using any double braces in JSX - <span>{{value}}</span> and change it to <span>{value}</span> (I did this mistake since I just moved from Angular to React). Check of return statements wrapped around braces like below and remove it. render() { let element = ( <span>text</span> ); return ( {element} // => Remove this extra brace ) } Check any other unintentional way you are using object in the JSX let nestedObjected = { a : '1', b : { c: '2' } }; {Object.keys(nestedObjected).map((key) => { return ( <div> <span>{nestedObjected[key]}</span> // This will cause issue since it will resolve to an object </div> ); })}

其他回答

类似的事情刚刚发生在我身上……

我写:

{response.isDisplayOptions &&
{element}
}

把它放在一个div固定:

{response.isDisplayOptions &&
    <div>
        {element}
    </div>
}

只需创建一个有效的JSX元素。在我的例子中,我将一个组件分配给一个对象。

const AwesomeButtonComponent = () => <button>AwesomeButton</button>
const next = {
  link: "http://awesomeLink.com",
  text: "Awesome text",
  comp: AwesomeButtonComponent
}

在我的代码的其他地方,我想动态分配那个按钮。

return (
  <div>
    {next.comp ? next.comp : <DefaultAwesomeButtonComp/>}
  </div>
)

我通过声明一个通过props comp初始化的JSX comp来解决这个问题。

const AwesomeBtnFromProps = next.comp
return (
  <div>
    {next.comp ? <AwesomeBtnFromProps/> : <DefaultAwesomeButtonComp/>}
  </div>
)

我只是让自己经历了一个非常愚蠢的错误版本,我不妨在这里为后代分享一下。

我有一些JSX是这样的:

...
{
  ...
  <Foo />
  ...
}
...

我需要注释掉它来调试一些东西。我在IDE中使用键盘快捷键,结果是这样的:

...
{
  ...
  { /* <Foo /> */ }
  ...
}
...

当然,这是无效的——对象作为react子对象是无效的!

我的问题是忘记了道具周围的花括号被发送到一个表示组件:

之前:

const TypeAheadInput = (name, options, onChange, value, error) => {

const TypeAheadInput = ({name, options, onChange, value, error}) => {

在我的例子中,这是因为必须在运行时注入动态数组。

我只是为对象添加了空检查,它工作得很好。

之前:

...
render(
...
    <div> {props.data.roles[0]} </div>
...
);

后:

...
let items = (props && props.data && props.data.roles)? props.data.roles: [];
render(
...
    <div> {items[i]} </div>
...
);