const robots = ['Bruce', 'Clark', 'Diana']
robots.map((robot, index) => {
return (
<h1 key={index}>{robot} </h1>
)
})
function MapComponent(){
const [myMap, setMyMap] = useState(new Map());
const updateMap = (k,v) => {
setMyMap(new Map(myMap.set(k,v)));
}
return(
<ul>
{[...myMap.keys()].map(k => (
<li key={k}>myMap.get(k)</li>
))}
</ul>
);
}
const todoItems = [
{
id: 1,
text:"todo 1"
},
{
id: 2,
text:"todo 3"
},
{
id: 3,
text:"todo 3"
}
];
const todoItems = todos.map((todo) =>
<li key={todo.id}>
{todo.text}
</li>
);
export const Articles = props => {
const [articles, setArticle] = React.useState(props.data || [])
React.useEffect(() => {
if (Array.isArray(articles) && articles.length < 1) {
setArticle([
{
title: 'how to map array in react',
content: `read this code!`,
},
])
}
}, [articles])
return (
<div>
{Array.isArray(articles) &&
articles.map((item, key) => (
<article key={key}>
<h2>{item.title}</h2>
<p>{item.content}</p>
</article>
))}
</div>
)
}