map state react code example
Example 1: react map
{array.map((item)=>{
return (
<div key={item.id}>I am one Object in the Array {item}</div>
)
})}
Example 2: map function in react
function NameList() {
const names = ['Bruce', 'Clark', 'Diana']
return (
<div>
{names.map(name => <h2>{name}</h2>)}
</div>
)
}
Example 3: new Map() collection in react state
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>
);
}
Example 4: react map
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((number) => number * 2);console.log(doubled);
Example 5: react create list of array in react
const App = props => { const quoteArray = props.quotes.map((quote) => { return ( <Quote text={quote.text} author={quote.author} /> ); }); return ( <div> <h2>Famous Quotes</h2> {quoteArray} </div> );};App.propTypes = { quotes: React.PropTypes.array}