My Dashboard component is a arrow function as shown below
const Dashboard= (props) => {
//users object is fetched
return(
<ul>
{
Object.keys(users).forEach((key,index) => {
console.log("iterating------>"+users[index].name);//this is iterating
return <li key={key}>hello</li>//this is not rendering
})
}
</ul>
)
}
This way it is not rendering to html. Please help
Tried my best but didn’t get the expected output
3
Answers
Use
map
instead offorEach
and it should workHere’s the corrected code:
Or use this (I prefer and use):
Hope this helps…
You are using forEach inside the JSX which does not return anything. In React to render a list you should use map instead that way it returns a new array that React can render as JSX elements.
You can use this example instead