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

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

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


当前回答

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

{{count}}

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

{count}

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

其他回答

多亏了zerkms的评论,我才注意到我的愚蠢错误:

我有onItemClick(e,项目)当我应该有onItemClick(项目,e)。

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

更多细节可以在这里找到: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>

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

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

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

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

之前:

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

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