The map function iterates an array and returns a
The
export const App = () => {
const countries = ['USA', 'Canada', 'Australia', 'Japan', 'India']
return(
<>
{countries.forEach(country => {
return <div>{country}</div> //does not work
})}
</>
)
}
export default App
For rendering elements in
import { ReactElement } from "react"
export const App = () => {
const countries = ['USA', 'Canada', 'Australia', 'Japan', 'India']
let forEachCountries: ReactElement[] = []
const getForEachCountries = (): ReactElement[] => {
countries.forEach(country => {
forEachCountries.push(<li>{country}</li>)
})
return forEachCountries
}
return(
<ul>
{getForEachCountries()}
</ul>
)
}
export default App
The map function iterates an array and returns a
export const App = () => {
const countries = ['USA', 'Canada', 'Australia', 'Japan', 'India']
return(
<ul>
{countries.map((country) => {
return <li>{country}</li>
})}
</ul>
)
}
export default App
If you don't intend to create a map out of array elements or don't intend to render the contents of an array in