I have two array of the same length, I would like to iterate on them and send the iterable to a new component.
The first array is haircutsLeft, the second is haircutsRight. Following this question: Using map to iterate through two arrays, I tried what they suggested with little success.
First try
export default function VotingComponent({haircutsLeft, haircutsRight}) {
return(
<Grid
{haircutsLeft.map((haircutLeft, idx) =>
<DisappearingComponent haircutLeft={haircutLeft} haircutsRight={haircutsRight[idx]} idx={idx}/>
)}
</Grid>
)
}
It does not pass
haircutsRight={haircutsRightLoop[idx]}to theDisappearingComponentbecause, if I console log it inDisappearingComponent, I getundefined
Second try
export default function VotingComponent({haircutsLeft, haircutsRight}) {
haircutsLeft.map((haircutLeft, index) => {
const haircutRight = haircutsRight[index];
return (
<div>
<DisappearingComponent haircutLeft={haircutLeft} haircutsRight={haircutRight}/>
</div>
);
});
}
I get
VotingComponent(...): Nothing was returned from render.
Third try - Following the comment's advice
export default function VotingComponent({haircutsLeft, haircutsRight}) {
return haircutsLeft.map((haircutLeft, index) => {
console.log(index)
const haircutRight = haircutsRight[index];
console.log(haircutRight)
return (
<div>
<DisappearingComponent haircutLeft={haircutLeft} haircutsRight={haircutRight}/>
</div>
);
});
}
I can console.log the
indexcorrectly, buthaircutRightgives meundefined
This is the component to which I am sending the props
export default function DisappearingComponent({haircutLeft, haircutRight}) {
console.log("haircutLeft", haircutLeft) // Correct value
console.log("haircutRight", haircutRight) // Undefined
return(
<div>
<Grid item xs={6}>
<LeftCard haircutLeft={haircutLeft} />
</Grid>
<Grid item xs={6}>
<RightCard haircutRight={haircutRight}/>
</Grid>
</div>
)
}
haircutsRighthas data in it? Also, is its length the same ashaircutsLeft? As I've mentioned in my answer, this could work only if your second array is of same size as the first one.haircutsarray of length 16 and I split it in half. However, now that you make me notice it,haircutsRightis empty even if in the grandparent component contains the right values. I will try to see what I did wrong. Thanks