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

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

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


当前回答

Found:带有键的对象

这意味着你传递的是一个键值。所以你需要修改你的处理程序:

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

您漏掉了大括号({})。

其他回答

我的案例在使用reduce时很常见,但这里没有分享,所以我发布了它。

通常,如果你的数组是这样的:

[{值:1},{值:2}]

你想要呈现这个数组中值的和。JSX代码如下所示

<div>{array.reduce((acc, curr) => acc.value + curr.value)}</div>

当数组只有一个项时,会出现这个问题,例如:[{value: 1}]。 (通常,当你的数组是来自服务器的响应时,这种情况发生,所以你不能保证数组中的项目数量)

当数组只有一个元素时,reduce函数返回元素本身,在这种情况下,它是{value: 1}(一个对象),它会导致不变性违反:对象是无效的React子错误。

你只是使用对象的键,而不是整个对象!

更多细节可以在这里找到:https://github.com/gildata/RAIO/issues/48

import React, { Component } from 'react'; import PropTypes from 'prop-types'; class SCT extends Component { constructor(props, context) { super(props, context); this.state = { data: this.props.data, new_data: {} }; } componentDidMount() { let new_data = this.state.data; console.log(`new_data`, new_data); this.setState( { new_data: Object.assign({}, new_data) } ) } render() { return ( <div> this.state.data = {JSON.stringify(this.state.data)} <hr/> <div style={{color: 'red'}}> {this.state.new_data.name}<br /> {this.state.new_data.description}<br /> {this.state.new_data.dependtables}<br /> </div> </div> ); } } SCT.propTypes = { test: PropTypes.string, data: PropTypes.object.isRequired }; export {SCT}; export default SCT; <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

try{
    throw new Error(<p>An error occured</p>)
}catch(e){
    return (e)
}

上面的代码产生了错误,然后我重写了它,像这样:

try{
    throw(<p>An error occured</p>)
}catch(e){
    return (e)
}

注意try块中new Error()的删除…

为了避免这个错误消息,编写代码的一个更好的方法是将一个字符串传递给throw new error()而不是JSX,并在catch块中返回JSX,就像这样:

try{
    throw new Error("An error occurred")
}catch(e){
    return (
        <p>{e.message}</p>
    )
}

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

导出默认的withAlert(警报)

这个

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

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

显然,正如其他人之前在这篇文章中提到的,在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中的值时,你将收到你所看到的不变违反错误。