1.

How to render an array of objects in react?

Answer»

Now, there are two ways to RENDER this object as an array,

First:

render() {
    const data =[{"name":"test1"},{"name":"test2"}];
    const listItems = data.MAP((d) => <li key={d.name}>{d.name}</li>);

    RETURN (
      <div>
      {listItems }
      </div>
    );
  }

Or, you COULD directly write the map function in the return

render() {
    const data =[{"name":"test1"},{"name":"test2"}];
    return (
      <div>
      {data.map(function(d, IDX){
         return (<li key={idx}>{d.name}</li>)
       })}
      </div>
    );
  }



Discussion

No Comment Found