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

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

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


当前回答

在我的例子中,我向我的子函数组件添加了一个async,并遇到了这个错误。不要使用带有子组件的async。

其他回答

问题是,当你渲染li时,你是在传递而不是将函数传递给你正在调用它的组件,所以一旦li被渲染,它就会调用它,你需要将引用传递给函数,而不是调用它来做这件事,如果没有参数可以传递,你可以这样写它

onClick={this.onItemClick.bind}

或者像这样,如果有争论要通过

onClick={()=>this.onItemClick.bind(this, item)}

在这种情况下,它将创建一个匿名函数来传递它的引用

显然,正如其他人之前在这篇文章中提到的,在React JSX中的道具。children不能为Object类型。这不是你具体问题的根本原因。

如果你仔细阅读错误文本,你会看到React在试图呈现一个匹配SyntheticEvent签名的对象时产生了错误:

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.

然而,有人想知道为什么要呈现SyntheticEvent,而这正是问题的真正答案所在。您显然不打算呈现SyntheticEvent,但是您的事件处理程序参数的顺序乱了。

在你的渲染方法中,你将onItemClick事件处理程序绑定到类组件的this,并将item作为参数传入:

render() {
    const items = ['EN', 'IT', 'FR', 'GR', 'RU'].map((item) => {
      return (<li onClick={this.onItemClick.bind(this, item)} key={item}>{item}</li>);
    });
// ...

根据Function.prototype的文档。bind时,在thisArg之后传递的所有参数都将被添加到稍后调用目标函数时传递的任何参数前:

Arg1, arg2,… 对象提供的参数前的参数 在调用目标函数时绑定函数。

然后查看事件处理程序,可以看到参数e列在参数项之前。

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

当onItemClick(e, item)被调用时,在绑定调用期间传入的项将在触发事件之前,因此参数e将被设置为映射和绑定的项,参数item将被设置为事件。

当setState被调用时,lang将被设置为代表触发onClick事件的synticevent,当你试图在其他地方呈现this.state.lang中的值时,你将收到你所看到的不变违反错误。

在使用Firebase的情况下,如果把它放在import语句的末尾不能工作,那么你可以尝试把它放在一个生命周期方法中,也就是说,你可以把它放在componentWillMount()中。

componentWillMount() {
    const firebase = require('firebase');
    firebase.initializeApp({
        //Credentials
    });
}

我花了半天时间来解决这个问题

我想根据用户是否登录显示一个数组

这就是我的代码 返回({userLogged ?userLoggedCart: cartItems} .map((item)=>{返回//无关代码})) 但这是错误的,如果你的代码是这样的,你就得把它改成

if (userLogged){return (userLoggedCart.map((item)=>{return //您的代码})}否则{cartItems.map((item)=>{return //您的代码})}

我在三元运算符中得到了一个错误。 我做了什么:

render(){
  const bar = <div>asdfasdf</div>
  return ({this.state.foo ? {bar} : <div>blahblah</div>})
}

结果是它应该是没有括号的bar,比如:

render(){
  const bar = <div>asdfasdf</div>
  return ({this.state.foo ? bar : <div>blahblah</div>})
}