当前位置: 首页 > 面试题库 >

尝试访问数组后,React组件返回无法读取null的属性'__reactInternalInstance $

席成仁
2023-03-14
问题内容

这是有问题的组件。

const UserList = React.createClass({
  render: function(){
    let theList;
    if(this.props.data){
      theList=this.props.data.map(function(user, pos){
        return (
          <div className="row user">
            <div className="col-xs-1">{pos}</div>
            <div className="col-xs-5">{user.username}</div>
            <div className="col-xs-3">{user.recent}</div>
            <div className="col-xs-3">{user.alltime}</div>
          </div>
        );
      }, this);
    } else {
     theList = <div>I don't know anymore</div>;
    }
    console.log(theList);
    return (
      theList
    );
  }
});

每当我尝试返回{theList}时,都会收到 无法读取属性’__reactInternalInstance $
mincana79xce0t6kk1s5g66r’的

错误。但是,如果我用静态html替换{theList},console.log将打印出我想要的正确对象数组。根据答案,我尝试同时返回{theList}和TheList,但这没有帮助。

在这两种情况下,console.log都首先打印出[],我认为这是因为componentDidMount包含我的ajax调用以从服务器获取json,并且尚未在第一个render()之前触发。我尝试检查this.props.data是否为null,但无济于事。

如果有帮助,以下是父组件:

const Leaderboard = React.createClass({
  getInitialState: function(){
    return ({data: [], mode: 0});
  },
  componentDidMount: function(){
    $.ajax({
      url: 'https://someurlthatreturnsjson',
      dataType: 'json',
      cache: false,
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error('https://someurlthatreturnsjson', status, err.toString());
      }.bind(this)
    });
  },
  render: function(){
    return (
      <div className="leaderboard">
        <div className="row titleBar">
          <img src="http://someimage.jpg"></img>Leaderboard
        </div> 
        <HeaderBar />
        <UserList data={this.state.data}/>
      </div>
    );
  }
});

问题答案:

好的,这里存在一些有趣的问题,但是您是 如此亲密
。具有react的大对象必须始终返回单个顶级元素(例如div)。因此,您的变量theList实际上是一个div数组。您不能直接退货。但是,如果将其包装在单个父div中,则可以将其返回。

const mockData = [

    {

    username: 'bob',

    recent: 'seven',

    alltime: 123,

  },

    {

    username: 'sally mae',

    recent: 'seven',

    alltime: 133999,

  },

];



var $ = {

    ajax(opt) {

    setTimeout(() => {

        opt.success(mockData);

    }, 200);

  }

}



const UserList = React.createClass({

  render: function(){

    let theList;

    if (this.props.data && this.props.data.length) {

      theList = this.props.data.map(function(user, pos){

        return (

          <div key={user.username} className="row user">

            <div className="col">{pos}</div>

            <div className="col">{user.username}</div>

            <div className="col">{user.recent}</div>

            <div className="col">{user.alltime}</div>

          </div>

        );

      });

    } else {

      theList = <div>There is NO data</div>;

    }



    return <div>{theList}</div>;

  }

});



const Leaderboard = React.createClass({

  getInitialState: function(){

    return ({data: [], mode: 0});

  },

  componentDidMount: function(){

    $.ajax({

      url: 'https://someurlthatreturnsjson',

      dataType: 'json',

      cache: false,

      success: function(data) {

        this.setState({data: data});

      }.bind(this),

      error: function(xhr, status, err) {

        console.error('https://someurlthatreturnsjson', status, err.toString());

      }.bind(this)

    });

  },

  render: function(){

    return (

      <div className="leaderboard">

        <UserList data={this.state.data}/>

      </div>

    );

  }

});



ReactDOM.render(

  <Leaderboard/>,

  document.getElementById('container')

);


.col {

  width: 200px;

  float: left;

}


<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>

<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>



<div id="container">

  <!-- This element's contents will be replaced with your component. -->

</div>

解释一点小提琴。不用担心看起来var $很奇怪的东西,我只是使用jQuery的ajax方法,这样我可以在200ms之后返回一些假数据。

另外,对我来说,jsfiddle在运行它时会给我一个“错误的配置”消息,但是我关闭了消息,结果就在那里。不知道那是什么。



 类似资料:
  • 我知道有类似的线程讨论范围问题。 使用以下组件 当您单击时,您会得到的属性'setState' 我知道你可以像这样做,但所有这些看起来都很奇怪,只是为了让它工作。 什么被认为是最优雅的方式来做到这一点?当然,人们必须有一个首选的方式,除了眼睛疼痛之外,还有其他好处?

  • 尝试使用以下查询检索MS团队的文件:https://graph.microsoft.com/v1.0/groups/{id}/drive…但我收到“拒绝访问” 我已确保授予以下权限(以及一些权限):文件。读取,文件。读写,文件。阅读全部,文件。读写。所有,站点。阅读所有,站点。读写。全部的 我只是想列出这些文件。我是否使用了错误的API调用?我还需要授予哪些其他权限?TIA。

  • 我已经创建了一个方法,它接受一个hashmap作为输入,将值转换成一个数组,使用气泡排序对数组进行排序,然后我希望它使用排序后的值从初始hashmap中选择前N个索引键。我知道这不是最好的方法,但这是好的(我写了这个方法作为一个替代)。 该方法返回null,因为maxItems为空,并且“System.out.println(Entry.GetKey());”不会将任何内容打印到控制台。 谢谢你的

  • 我是springboot的新手,我正在尝试从application.properties文件的位置(src/main/resources)读取属性值。但它总是返回NULL。我也需要帮助。附加类和属性文件。请注意:我试过“https://www.baeldung.com/properties-with-spring”如何访问Spring Boot中application.properties文件中定

  • 问题内容: 您好,我正在尝试将React-Router与Firebase配合使用。但这给了我这个错误。 无法读取null的属性“ props” 这正是代码,我正在使用我的react-router 向下代码在组件上作为loginpanel组件。 我的react路由器在main.jsx上声明,我的react-router看起来像这样。 问题答案: 在回调中,指针不再指向您的React组件。有很多方法可

  • 我收到以下JavaScript错误: TypeError:无法读取null的属性“title” 代码如下: mds-iMac: cmscart imac$nodemo app[nodemo] 1.11.0[nodemo]随时重启,输入[nodemo]观看:.[nodemo]启动(node: 2274)DeprecationWarning:在猫鼬中被弃用 [nodemon]应用程序崩溃-正在等待文件