1

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 the DisappearingComponent because, if I console log it in DisappearingComponent, I get undefined

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 index correctly, but haircutRight gives me undefined

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>
    )

}
3
  • Your third try should work. Does your haircutsRight has data in it? Also, is its length the same as haircutsLeft? As I've mentioned in my answer, this could work only if your second array is of same size as the first one. Commented Feb 9, 2020 at 15:32
  • 1
    Yes, both are from the same length. In the grandparent component I have a haircuts array of length 16 and I split it in half. However, now that you make me notice it, haircutsRight is empty even if in the grandparent component contains the right values. I will try to see what I did wrong. Thanks Commented Feb 9, 2020 at 15:35
  • Happy I could help you figure that out. Good luck with it! Commented Feb 9, 2020 at 15:36

1 Answer 1

2

On your first try I can't spot your haircutsRightLoop anywhere. I think you are meant to use haircutsRight there. If both your haircutsLeft and haircutsRight arrays are of the same length, then your code should do the trick.

On your second try, you should also return your new array returned by .map():

export default function VotingComponent({haircutsLeft, haircutsRight}) {

     return haircutsLeft.map((haircutLeft, index) => {
         const haircutRight = haircutsRight[index];
         return (
             <div>
             <LeftCard haircutLeft={haircutLeft}/>
             <RightCard haircutRight={haircutRight} />
             </div>    
         );
     });
}

Sign up to request clarification or add additional context in comments.

8 Comments

(and add a render method)
OP seems to work with functional component. render method is part of class components.
Yes, sorry. In first try I did haircutsRight, I corrected it. However, no success. I am using Hooks
(and both components params need to be refered to as "props", they're currently anonymous)
Yes, I updated my question merging in my third try your suggestions
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.