Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  80] [ 5]  / answers: 1 / hits: 7165  / 3 Years ago, thu, december 16, 2021, 12:00:00

const test = () = {

const array = ['hello', 'myNameIs']

return (

{
array.map((arr) => (
<div>{arr}</div>
<button>Edit</button>
)
}

)

}

This .map() method is not working as I intended.


With the code, I was trying to get


hello [button]
myNameIs [button]

like this.


But when I actually render the code, I get


hello MynameIs [button]

In this kind of situation, How can I change the .map() statement?


Should I use an index?


More From » reactjs

 Answers
3

You are not using the correct syntax for the arrow function
It should be like
const test = ()=>{}
React component JSX should return one parent container try wrapping it like:




 const Test = () => {
const array = [hello, myNameIs];

return (
<>
{array.map((arr) => {
return (
<>
<div>{arr}</div>
<button>Edit</button>
</>
);
})}
</>
)
}




Here is the working code sandbox link https://codesandbox.io/s/kind-easley-jxiei?file=/src/App.js:57-336


I Hope it helps.


[#582] Tuesday, December 7, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;