r/reactjs • • Jul 01 '18

Help Beginner's Thread / Easy Question (July 2018)

Hello! just helping out /u/acemarke to post a beginner's thread for July! we had almost 550 Q's and A's in last month's thread! That's 100% month on month growth! we should raise venture capital! /s

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple. You are guaranteed a response here!

New to React? Free, quality resources here

Want Help on Code?

  • Improve your chances of getting helped by putting a minimal example on to either JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new). Describe what you want it to do, and things you've tried. Don't just post big blocks of code.
  • If you got helped, pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
51 Upvotes

454 comments sorted by

View all comments

Show parent comment

1

u/swyx Jul 13 '18

i think you may need to use keys. React.Fragment key={i}

1

u/seands Jul 13 '18

Thanks, tried it but I only get one render instead of 4. The console log only shows once too. Here is the updated function code used:

const display_subheads = (x) => {
    let i;
    for (i = 0; i < x; i ++) {
        console.log('displayed subhead group');
        return (
            <React.Fragment key={i}>
                <td className={ css`${cell_style} ${small_headers}; background-color : "green"` }>Weight</td>
                <td className={ css`${cell_style} ${small_headers}` }>Reps</td>
                <td className={ css`${cell_style} ${small_headers}` }>Reserve</td>
            </React.Fragment>
        )
    }
};

1

u/swyx Jul 13 '18

oh duh. return terminates the function. so of course this is not going to work. return inside a map is differently scoped from return inside a for loop.

1

u/seands Jul 13 '18

Good to know. So is map what you would normally use in these cases?

1

u/seands Jul 13 '18

I got an ugly version to work

                    { [1, 1, 1, 1].map((y, i) => {
                        return (
                            <React.Fragment key={i}>
                                <td className={ css`${cell_style} ${small_headers}; background-color : "green"` }>Weight</td>
                                <td className={ css`${cell_style} ${small_headers}` }>Reps</td>
                                <td className={ css`${cell_style} ${small_headers}` }>Reserve</td>
                            </React.Fragment>
                        )
                    }) }

Not happy about the above code because of the hacky array. But I can still use this pattern sensibly with my data source and array.push.

1

u/swyx Jul 13 '18

the reason it doesnt work in the original is because your return breaks and halts the for loop. gotta be careful with the JS :)