0

I need to render one jsx component for each item in an array.

import { ListView } from './components'; // Custom component

const array = ["item1","item2","item3"];

export default function App() {
    return(
      <div>
        {/* Here i want to render a <ListView /> component for each of the items in the array. */}
     </div>
   );
}

Here there are 3 items in the array so i want to render 3 different components.

Any help will be appreciated.

1 Answer 1

5

Try to use map method:

return (
 <div>{ array.map(item => <ListView key={item} item={item} /> }</div>
)
Sign up to request clarification or add additional context in comments.

3 Comments

you are not passing item to ListView
lol, react has so beautiful code :D
@thedude yes becuse i don't now the what is inside of ListView. I just want to explain how we display a component using an array. But you are right we can add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.