If we have a Typescript Map object, how do we iterate it in order to render a number of items in React?
Using .map on the Map object causes Typescript to complain:
export function Foo() {
type MyMap = Map<string, number>;
const myMap: MyMap = new Map<string, number>();
return (
<div>
{
myMap.map(x => {
return (
<Foo key={x.id} score={x.score} />
)
})
}
</div>
)
}
Thanks!
.mapmethod. You also need to enclose JS expressions in{}delimiters when inside JSX in order to interpolate.mapmethod? Otherwise, is there another method that Map has that we can use for this?.mapisn't one of them)Array.from(myMap.entries())allowed me to use the.map()method again. Is this a suggested way to do it?Array.from(myMap)is equivalent toArray.from(myMap.entries()), which is also equivalent to[...myMap].