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

ast*_*ern 10 javascript arrays ajax reactjs react-jsx

这是有问题的组件.

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
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

每当我尝试返回{theList}时,我都会收到一个无法读取属性'__reactInternalInstance $ mincana79xce0t6kk1s5g66r'的null错误.但是,如果我用静态html替换{theList},console.log将打印出我想要的正确对象数组.根据答案,我试图返回{theList}和List,但这没有帮助.

在这两种情况下,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>
    );
  }
}); 
Run Code Online (Sandbox Code Playgroud)

Dav*_*son 8

好的,这里有一些有趣的问题,但你是如此接近.最重要的是,你必须总是返回一个顶级元素(例如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')
);
Run Code Online (Sandbox Code Playgroud)
.col {
  width: 200px;
  float: left;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

稍微解释一下小提琴.不要担心奇怪的var $东西,我只是简化了jQuery的ajax方法,所以我可以在200ms后返回一些假数据.

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