Nope JSX doesn't have a built-in loop construct. Instead it offloads looping to JavaScript & just accepts an array of JSX elements for rendering lists

๐Ÿ’ก So in React, to loop we gotta convert an array of data โžก๏ธ array of JSX elements

Let's look at some ways how...

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
Calling `.map` on the array is the most popular way to generate that array of JSX elements

And cuz `.map` returns a new array we can inline it directly in the JSX w/o using a var (also very common)

Putting the `.map` inline makes it *feel* like traditional loop templates

Using a regular `for` loop (or `for-of`, `.forEach`, etc) requires a var that we gotta `.push` into

`for` is likely more familiar to JS newcomers but it means we cannot inline the code

...unless we wanna go rogue and put it in an IIFE ๐Ÿคฏ (anyone used one here before???)

Buuut if you really want looping in your JSX, babel-plugin-jsx-control-statements adds a JSX element to enable it

There's nothing to import in the source code. Just use the `<For>` elementโ€ฆ

This was a common approach in React's early days

Lastly, what if our data source isn't an array (or object)? Like a number or something? We can use a `for` loop directly

But since I prefer functional-style programming I first convert that number into an array, then use good ol' `.map`

If you're new to React, hopefully this info can clarify looping for you

Or if you already know React, it'll provide examples of how to explain it to those teammates you're training ๐Ÿ™Œ๐Ÿพ

You can also share this post w/ the full details

I've got a weekly newsletter where I share this type of content. It's not the link aggregator type, but it contains a brand new post & an old one I share

It's all about the frontend: JS/TS, React, DivOps, CSS & more! Subscribe to get your learn on ๐Ÿ˜‰โ€ฆ

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh

Keep Current with Ben Ilegbodu ๐Ÿ€๐Ÿ‘จ๐Ÿพโ€๐Ÿ’ป

Ben Ilegbodu ๐Ÿ€๐Ÿ‘จ๐Ÿพโ€๐Ÿ’ป Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!


Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @benmvp

23 Mar
Here is a quick React custom Hook that supports copying text to the system clipboard. It also returns a copied/failed status that can be reflected in the UI

(I remember the days when we could only do this w/ Flash ๐Ÿ˜ญ)

more details in the thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
useCopyToClipboard returns a copy function that the UI calls when the user wants to copy the text (likely a button click). It updates the status based on the success/failure of writing to the clipboard

It uses useCallback to ensure a stable function reference

It also uses useEffect to set a timeout that will reset the status after a specified amount of time. This basically makes the status fleeting, which is a typical user experience

Read 6 tweets
26 Jan
Have y'all gotten that React warning about state updates after component unmount?

I get it mainly when running unit tests

Happens when:
- Make async request
- Component unmounts
- Response comes back
- Try to update state w/ new data

I've come up w/ 4๏ธโƒฃ solutions

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
Since vanilla JS Promises aren't cancellable the next best thing is to keep track of the mounted state & don't update state if the comp is unmounted

1๏ธโƒฃ Variable to track mounted state

Quick & dirty, but only works in single useEffect w/ no deps

2๏ธโƒฃ Ref to track mounted state

All effects can check the ref regardless of if they have deps. Will have to duplicate in multiple components tho

Read 8 tweets
14 Jan
If you're getting started with React in TypeScript one of the first things you'll wanna do is migrate from runtime prop-types over to static TS types

Typing a function component is the same as any function in TS

Typically props are typed with a TS `interface`

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
The primitive prop types map over to their TS equivalents...

Special types like React nodes, enums, class instances, and others...

Read 7 tweets
5 Jan
Using objects/arrays in deps for React useEffect can cause it to run the effect on *every* re-render even if contents are the same cuz the references are different

We've got a couple of options...

1๏ธโƒฃ Depend on the obj properties instead of the entire obj

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ ImageImage
2๏ธโƒฃ Duplicate the object within the effect when the *entire* object is needed. useEffect then depends on the pieces needed to create the object

In general creating object literals is cheap so the perf wins from optimized useEffect should outweigh the duplication

๐Ÿงต๐Ÿ‘‡๐Ÿพ Image
3๏ธโƒฃ Memoize/cache the object w/ useMemo Hook if creating the object is too expensive to do it twice. The obj is now safe to use in deps

This is suggested by the `react-hooks/exhaustive-deps` ESLint rule (part of `eslint-plugin-react-hooks` which you should totally be using)

๐Ÿงต๐Ÿ‘‡๐Ÿพ Image
Read 6 tweets
15 Dec 20
I used to tell folks learning React to learn JS really well first. I had good intentions but was still gate-keeping ๐Ÿ˜”

But I do think knowing modern JS can really level-up your React skills and I've got top 10 features I think will help you be more awesome-r ๐Ÿ”ฅ๐Ÿ”ฅ

[๐Ÿงต thread]
1. ES Modules

I see lots of confusion about when to use named vs default imports. The import style depends on how it was exported (named or default)

Named imports go within the curly braces & the single default import is outside of them ๐Ÿค“ ImageImage
2. Arrow functions

Arrow functions can exist everywhere within your React app. Some folks (like myself) replace all functions with arrows while others only use them in spots

They really win w/ character savings but for those unfamiliar w/ the syntax they can lose w/ clarity ๐Ÿง ImageImage
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!

This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!