Im very new to javascript, but like hgb123's answer, something like this inside a render or return block:
<div className="myClass">
{myList.map((eachItemInList) => (
<ReactComponent certainProp={eachItemInList} />
))}
</div>
works perfectly for a list like this one:
const myList = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']
hope this helped someone!
Answer from Roberto Rios on Stack OverflowVideos
Im very new to javascript, but like hgb123's answer, something like this inside a render or return block:
<div className="myClass">
{myList.map((eachItemInList) => (
<ReactComponent certainProp={eachItemInList} />
))}
</div>
works perfectly for a list like this one:
const myList = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']
hope this helped someone!
You should wrap the items first and render the grouped ones
const groupItems = items =>
items.reduce((groupedItems, item) => {
if (!groupedItems[item.group]) {
groupedItems[item.group] = []
}
groupedItems[item.group].push(item)
return groupedItems
}, {})
const items = Object.entries(groupItems(arr.items)).map(
([groupName, items]) => (
<React.Fragment>
<li>{groupName}</li>
{items.map(item => (
<li>{item.item}</li>
))}
</React.Fragment>
)
)
» npm install google-map-react
» npm install react-map-to-components
I'll skip straight to the question right away. If you are in a Parent component, and you are mapping through an array to create Child components....and inside the Child component, you have a mounting function.....does that mounting function run during every iteration of the Parent's map? Or does React batch collect all Child components and mount them on the DOM after it finishes the entire map?
When I look at the readings from my console, it looks like the mounting code in the Child component is running only after the Parent is done with the map.
To be more specific, if my array is 10 values long, the console shows 10 statements of " arr Passes Test ", and only after that it displays " mount hook called ".
Why is this happening? I was expecting the console to first log the statement from Parent and then log the statement from Child after mounting it. Not batch collect it for the map and mount all 10 Childs at once.
Is there a way to prevent this behavior?
(Ps: I need it to mount on each iteration because I want to update a state variable before it renders future components in that same map)
function Parent(){
return(
arrPassesTest(arr) ?
arr.map( (val, index) => {
return (<Child val={val} index={index}/>)
} )
)
}
function arrPassesTest(arr){
console.log("arr Passes Test");
return true;
}
------------
function Child(){
useEffect(()=>{
console.log("mount hook called");
},[])
}Gosha Arinich is right, you should return your <li> element.
But, nevertheless, you should get nasty red warning in the browser console in this case
Each child in an array or iterator should have a unique "key" prop.
so, you need to add "key" to your list:
this.state.data.map(function(item, i){
console.log('test');
return <li key={i}>Test</li>
})
or drop the console.log() and do a beautiful oneliner, using es6 arrow functions:
this.state.data.map((item,i) => <li key={i}>Test</li>)
IMPORTANT UPDATE:
The answer above is solving the current problem, but as Sergey mentioned in the comments: using the key depending on the map index is BAD if you want to do some filtering and sorting. In that case use the item.id if id already there, or just generate unique ids for it.
You are not returning. Change to
this.state.data.map(function(item, i){
console.log('test');
return <li>Test</li>;
})