【问题标题】:React Invariant Violation: Objects are not valid as a React childReact Invariant Violation:对象作为 React 子对象无效
【发布时间】:2017-02-09 21:22:40
【问题描述】:

我有以下代码:

import ReactDom from 'react-dom';
import React from 'react';
import {render} from 'react-dom';
import $ from 'jquery';


class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: '',
            loading: true
        }
    }
    componentDidMount () {
        const newsfeedURL = 'https://s3-eu-west-1.amazonaws.com/streetlife-coding-challenge/newsfeed.json';
        $.get(newsfeedURL, function(result) {
            this.setState({
                data: JSON.parse(result),
                loading: false
            });
            console.log(typeof this.state.data.messages);
        }.bind(this));
    }
    render () {
      let content;
      if (this.state.loading === false && this.state.data.messages) {
        content = Object.keys(this.state.data.messages).map(key => {
         return <div key={key}>Key: {key}, Value: {this.state.data.messages[key]}</div>;
        })
      } else { 
        content = ''; // whatever you want it to be while waiting for data
      }
      return (
        <div>
          {content}
        </div>
      )
    }
}


ReactDom.render(
  <App />,
  document.getElementById('app')
);

但我收到以下错误:

未捕获的不变违规:对象作为 React 子级无效(发现:具有键 {body、附件、视频、主题、updated_at、id、主题、downvotes、author、posted_at、cmets、user_vote、upvotes 的对象、状态、标签、位置、track_impact、user_is_following、cmets_count})。如果您打算渲染一组子项,请改用数组或使用 React 附加组件中的 createFragment(object) 包装对象。检查App的渲染方法。

我看过这个答案,但对我的情况没有帮助:Invariant Violation: Objects are not valid as a React child

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    在您的 div 中,您正在尝试渲染 Value: {this.state.data.messages[key]} 这是一个对象。你不能使用 React 的 JSX 直接渲染对象。但是,您可以呈现的是该对象中保存的一些实际原始数据类型(例如字符串、数字),例如 Value: {this.state.data.messages[key].body} 将呈现对象的 body 属性中保存的字符串值。这是一个演示:http://codepen.io/PiotrBerebecki/pen/bwowxP

    class App extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                data: '',
                loading: true
            }
        }
        componentDidMount () {
            const newsfeedURL = 'https://s3-eu-west-1.amazonaws.com/streetlife-coding-challenge/newsfeed.json';
            $.get(newsfeedURL, function(result) {
                this.setState({
                    data: JSON.parse(result),
                    loading: false
                });
                console.log(typeof this.state.data.messages);
            }.bind(this));
        }
        render () {
          let content;
          if (this.state.loading === false && this.state.data.messages) {
            content = Object.keys(this.state.data.messages).map(key => {
             console.log(this.state.data.messages[key])
             return <div key={key}><b>Key: {key},</b> Value: {this.state.data.messages[key].body}</div>;
            })
          } else { 
            content = ''; // whatever you want it to be while waiting for data
          }
          return (
            <div>
              {content}
            </div>
          )
        }
    }
    
    
    ReactDOM.render(
      <App />,
      document.getElementById('app')
    );
    

    【讨论】:

      【解决方案2】:

      class App extends React.Component {
          constructor(props) {
              super(props);
              this.state = {
                  data: '',
                  loading: true
              }
          }
          componentDidMount () {
              
              const newsfeedURL = 'https://s3-eu-west-1.amazonaws.com/streetlife-coding-challenge/newsfeed.json';
                      $.get(newsfeedURL, function(result) {
                          this.setState({
                              data: result,
                              loading: false
                          });
                      }.bind(this));
          }
          render () {
            let content;
            if (this.state.loading === false && this.state.data.messages) {
              content = this.state.data.messages.map((ele,key) => {
               return <div key={key}>id: {this.state.data.messages[key].id}</div>;
              })
            } else { 
              content = ''; // whatever you want it to be while waiting for data
            }
            return (
              <div>
                {content}
              </div>
            )
          }
      }
      
      
      ReactDOM.render(
        <App />,
        document.getElementById('app')
      );
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <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>
      <div id="app"></div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-14
        • 2019-08-18
        • 2019-07-20
        • 2019-03-09
        相关资源
        最近更新 更多