1

I am trying to iterate through the returned array from render inside return.

class Example extends React.Component {
  render() {
    let actortype = 1;
    const arr1 = ['a', 'b', 'c'];
    const arr2 = ['x', 'y', 'z'];
    if (actortype == 1) {
      var array = [...arr1]; //clone arr1 to array
    } else {
      var array = [...arr2];
    }
    return (
      <div>
           //I want to map through the array returned above in render. I am doing something like this:
                array.map((index)=>{
                <div>testing</div>      
            })
         // seems not the right approach to  access array inside return. returns plain text
      </div>
    );
  }
}

Please share your feedback, how can I do that.

Mohsin
  • 73
  • 2
  • 9

5 Answers5

3

You use a JSX expression ({}) within the div, around the call to map:

<div>
    {array.map( item => <div>{item}</div>})}
</div>

and either use return or use a concise arrow function (I've done the latter above). For more on this latter point, see Why doesn't my arrow function return anything?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

you are doing it right :

<div>
     {array.map( item =>{
      return <div>{item}</div>      
    })}
</div>

For condition above you can just use tertiary operator i.e.

    let actortype = 1;
    const arr1 = ['a', 'b', 'c'];
    const arr2 = ['x', 'y', 'z'];
    const array = actortype == 1 ? arr1 : arr2;
Muhammad Haseeb
  • 1,269
  • 12
  • 22
0
<div>
     {array.map(item =>(
      <div>{item}</div>      
     )}
</div>
Tan Dat
  • 2,888
  • 1
  • 17
  • 39
0

You have done some mistakes. The map must need to return something. And rendering some HTML inside the map needs a key property.

class Example extends React.Component {
  render() {
    let actortype = 1;
    const arr1 = ["a", "b", "c"];
    const arr2 = ["x", "y", "z"];

    // Declare it here
    let array = [];

    if (actortype == 1) {
      array = [...arr1]; //clone arr1 to array
    } else {
      array = [...arr2];
    }
    return (
      <div>
        {array.map((value, index) => {
            // must return inside map and use a key property is a must
            return (<div key={index}>testing: {value}</div>)
        })}
      </div>
    );
  }
}
ReactDOM.render(<Example />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="app"></div>
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
0

You should awoid using a name array as a name of your variable and name index as a name of variable in your loop.. so the code should look like that..:

class Example extends React.Component {
  render() {
    let actortype = 1;
    const arr1 = ['a', 'b', 'c'];
    const arr2 = ['x', 'y', 'z'];
    if (actortype == 1) {
      var new_array = [...arr1]; //clone arr1 to array
    } else {
      var new_array = [...arr2];
    }
    return (
      <div>
           //I want to map through the array returned above in render. I am doing something like this:
                new_array.map((array_item)=>{
                <div>{array_item}</div>      
            })
         // seems not the right approach to  access array inside return. returns plain text
      </div>
    );
  }
}
Mario Boss
  • 1,784
  • 3
  • 20
  • 43