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

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

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


当前回答

所发生的是您试图实现的onClick函数立即被执行。 因为我们的代码不是HTML,它是javascript,所以它被解释为一个函数执行。 onClick函数接受函数作为参数,而不是函数执行。

const项=[“EN”,“它”,“FR”、“GR”,“俄罗斯”). map((项)= > { 返回(<李onClick = {(e) = > onItemClick (e,项)}关键={项}>{项}< /李>); });

这将在List Item上定义一个onClick函数,该函数将在单击它后执行,而不是在组件呈现时立即执行。

其他回答

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

只需删除return语句中的花括号。

之前:

render() {
    var rows = this.props.products.map(product => <tr key={product.id}><td>{product.name}</td><td>{product.price}</td></tr>);
    return {rows}; // unnecessary
}

后:

render() {
    var rows = this.props.products.map(product => <tr key={product.id}><td>{product.name}</td><td>{product.price}</td></tr>);
    return rows; // add this
}

如果你把async放在一个函数组件(可以是一个玩笑和反应测试库测试的包装器,也可以是一个普通的旧函数组件)前面,就像这样:

const MockComponent = async () => {
  <Provider>
    <ComponentImGoingToTest />
  </Provider>
}

你期望能够像这样渲染它:

<MockComponent />

然后是两件事:

你不理解async的含义。 由于这个关键字,调用包装器函数将调用promise(它是一个对象),而不是JSX归结为预期的React函数。

我只是得到了相同的错误,但由于不同的错误:我使用了双括号,如:

{{count}}

插入count的值而不是正确的值:

{count}

编译器可能将其转换为{{count: count}},即试图将一个对象作为React子对象插入。

这是我的代码:

class App extends Component {
  constructor(props){
    super(props)
    this.state = {
      value: null,
      getDatacall : null
    }
    this.getData = this.getData.bind(this)
  }
  getData() {
  //   if (this.state.getDatacall === false) {
    sleep(4000)
    returnData("what is the time").then(value => this.setState({value, getDatacall:true}))
    // }
  }
  componentDidMount() {
    sleep(4000)

    this.getData()
  }
  render() {
    this.getData()
    sleep(4000)
    console.log(this.state.value)
    return (
      <p> { this.state.value } </p>
    )
  }
}

我就遇到了这个错误。我不得不把它改成

 render() {
    this.getData()
    sleep(4000)
    console.log(this.state.value)
    return (
      <p> { JSON.stringify(this.state.value) } </p>
    )
  }

希望这能帮助到一些人!