StochasticNerd StochasticNerd - 1 year ago 71
React JSX Question

List elements not rendering in React

This is my Sidebar component.

const Sidebar = React.createClass({
let props = this.props;
<div className= 'row'>
<h2>All Rooms</h2>
{, i) => {
<li key={i}> {} </li>
{props.addingRoom && <input ref='add' />}


This is where I render it populating one room.

<Sidebar rooms={[ { name: 'First Room'} ]} addingRoom={true} />
</App>, document.getElementById('root'));

The contents inside the
tag don't render at all. Any idea what I could be missing.

Answer Source

You aren't returning anything from the map function, so it renders nothing inside the unordered list. In your arrow function, you do:

(room, i) => {

This doesn't return anything. requires a callback that returns a value to do anything. map transform elements to the array, mapping (executing) the callback on each element. The return value is what the value is in the corresponding position in the returned and mapped array.

You must return the list element in the map function like so:

    {, i) => {
      return <li key={i}> {} </li>;

Now, the mapped array of list elements is rendered because the list element is returned.

To make this shorter, you may write it in the form (params) => value which is equivalent to the above, where value is the returned value, like so:

{, i) => <li key={i}> {} </li>)}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download