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

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

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


当前回答

对于那些提到Stringify()和toString()作为解决方案的人,我会说这对我有用,但我们必须理解问题以及为什么会发生。在我的代码中,这是一个简单的问题。我有2个按钮调用相同的函数,但其中一个按钮没有正确地将参数传递给该函数。

其他回答

我得到了同样的错误,我改变了这个

导出默认的withAlert(警报)

这个

导出默认withAlert()(警报)。

在旧版本中,前一个代码是可以的,但在后来的版本中,它抛出一个错误。所以使用后面的代码来避免错误。

我有同样的问题,因为我没有把道具放在花括号里。

export default function Hero(children, hero ) {
    return (
        <header className={hero}>
            {children}
        </header>
    );
}

所以如果你的代码和上面的类似,你就会得到这个错误。 要解决这个问题,只需在道具周围加上花括号。

export default function Hero({ children, hero }) {
    return (
        <header className={hero}>
            {children}
        </header>
    );
}

试试这个

 {items && items.title ? items.title : 'No item'}

只需删除组件中的async关键字。

const Register = () => {

这之后就没有问题了。

如果您正在使用Firebase并看到此错误,则值得检查是否导入正确。从5.0.4版本开始,你必须像这样导入它:

import firebase from '@firebase/app'
import '@firebase/auth';
import '@firebase/database';
import '@firebase/storage';

是的,我知道。我在这上面也浪费了45分钟。