Here is the problematic component in question.
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 ); } });
Whenever I try to return {theList}, I get I can not read the '__reactInternalInstance $ mincana79xce0t6kk1s5g66r' property of null . However, if I replace {theList} with static html, console.log will output the correct array of objects that I want. According to the answers, I tried to return both {theList} and theList, but that did not help.
In both cases, console.log first outputs [], which I assume because componentDidMount contains my ajax call to get json from the server and has not quit before the first render () yet. I tried checking this.props.data is null, but this does not help.
Here is the parent component if it helps:
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> ); } });
javascript arrays ajax reactjs react-jsx
astringentpattern
source share