Colin Aulds Colin Aulds - 20 days ago 5
React JSX Question

react components not talking to each other

I've built a basic restuarant recommendation app that filters by location using the YELP api. The api was responding to my requests with the response object and everything was appending to my divs perfectly, but I realized that for my project, I needed to make a new layer for the data listing. Here are the relevant portions of my two components as they are now:

display-recs:

var DisplayRecs = React.createClass({
render: function() {
var recsLoop = [];

if (this.props.recommendations) {
for (var i=0; i < this.props.recommendations.length; i++) {
recsLoop.push(<Recommendations item={this.props.recommendations[i]} />)
}
}
console.log(this.props.recommendations);
return (
<div className="DisplayRecs">
{recsLoop}
</div>
);
}
});

var mapStateToProps = function(state, props) {
return {
recommendations: state.recommendations
};
};


recommendations:

var Recommendations = React.createClass({
render: function() {
<div id="bizData">
<div id='nameList'>{this.props.item.name}</div>
<div id='phoneList'>{this.props.item.phone}</div>
<div id='ratingList'>{this.props.item.rating}</div>
</div>
}
});
var mapStateToProps = function(state, props) {
return {
recommendations: state.recommendations
};
};


I cannot figure out why the nameList, phoneList, and ratingList will not print onto the dom. When I view the elements tab in my devtools, all i see is an empty displayrecs div. I've tried to just change things by guessing, but it's not been fruitful. Can any of you see an obvious problem with the current code?

Thanks

Answer

Your Recommendations react component's render function doesn't have any return statement. Try doing this:

 var Recommendations = React.createClass({
    render: function() {
     return ( <div id="bizData">
                <div id='nameList'>{this.props.item.name}</div>
                <div id='phoneList'>{this.props.item.phone}</div>
                <div id='ratingList'>{this.props.item.rating}</div>
              </div>);
   }
 });

Also add a key to the Recommendations components as @Vikramaditya recommends:

 recsLoop.push(<Recommendations key={i} item={this.props.recommendations[i]}  />)