Ben Ilegbodu ๐Ÿ€๐Ÿ‘จ๐Ÿพโ€๐Ÿ’ป Profile picture
May 19, 2021 โ€ข 7 tweets โ€ข 4 min read โ€ข Read on X
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

github.com/AlexGilleran/jโ€ฆ

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

๐Ÿงต๐Ÿ‘‡๐Ÿพ

benmvp.com/blog/looping-iโ€ฆ
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 ๐Ÿ˜‰

benmvp.com/subscribe/?utmโ€ฆ

โ€ข โ€ข โ€ข

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!

PDF

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

Dec 23, 2021
I recently created a TypeScript generic utility type that recursively converted `Date` types into Firestore `Timestamp` types

The type was fun to come up w/ cuz I learned a lot but it's also a bit complex. Lemme break down all the TS features line by line...

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
`ToFirestore<>` takes a single generic type param, `MaybeDate`. If it is a `Date` type, then the "true" branch of the type conditional returns a `Timestamp` type instead. This base case of the recursive type serves as the crux of the mapping of `Date` โžก๏ธ `Timestamp` type

๐Ÿงต๐Ÿ‘‡๐Ÿพ
The "false" branch of the outer conditional begins a nested one that converts `Date` โžก๏ธ `Timestamp` types of an array type by recursively calling `ToFirestore<>` on the array item type

(the `infer` keyword auto-creates a new generic type that's the array items type)

๐Ÿงต๐Ÿ‘‡๐Ÿพ
Read 8 tweets
Nov 18, 2021
Something that always comes up when I teach React + TypeScript is "interfaces vs type aliases" for props & other object type definitions

95%+* of the time they're interchangeable. But let's look at practical cases where we might favor one over the other ๐Ÿ‘€

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
First the similarities. Both interfaces & type aliases can...

โœ… Define an object type (duh!)
โœ… Merge/extend either interfaces or type aliases
โœ… Use generic types

๐Ÿงต๐Ÿ‘‡๐Ÿพ
We need type aliases for...

โžก๏ธ Defining discriminated unions
โžก๏ธ Extending discriminated unions
โžก๏ธ Using built-in utilities or other custom generic types

(technically interfaces can extend utility types but it's ugly IMO ๐Ÿคฎ)

๐Ÿงต๐Ÿ‘‡๐Ÿพ
Read 7 tweets
Oct 28, 2021
TypeScript's type system is very powerful because it allows us to express types in terms of other types, including generics

I was looking at the implementation of the TS utilities & learned lots about how to implement generics, so I wanted to share my learnings

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
1๏ธโƒฃ Basic generics

Generic types are like implicit return funcs. They take 1+ params & return a new type

`Partial<>`, `Required<>` & `Readonly<>` take any obj type & return a new obj type w/ property modifiers

We can use them as an example for our own `Mutable<>` util!

๐Ÿงต๐Ÿ‘‡๐Ÿพ
2๏ธโƒฃ Generic constraints

Using `extends` we can restrict what types of params are allowed. It's kinda like providing types of our generic params

`Record<>` limits the `Keys` param to only `string`, `number` or `symbol` cuz JS objects only allow those types as keys

๐Ÿงต๐Ÿ‘‡๐Ÿพ
Read 9 tweets
Oct 13, 2021
What are some situations for when we can use the `useCallback()` & `useMemo()` Hooks in React components?

Well, one case where we need `useCallback()` is when we call a helper function w/in `useEffect()`, so we need "referential equality" to include it in deps

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ
I also use `useCallback()` by default when returning a function from a custom Hook cuz I dunno how that function will be used w/in host components

`useCallback()` gives a stable function reference similar to the updater func from `useState()` (the 2nd array element)

๐Ÿงต๐Ÿ‘‡๐Ÿพ
The `useMemo()` Hook is similar to `useCallback()` except that it memoizes any value not just functions

So I use `useMemo()` in the same situations: when I have a derived object/array that's used in the deps of `useEffect()`

(I like to think of "memoization" as a cache)

๐Ÿงต๐Ÿ‘‡๐Ÿพ
Read 8 tweets
Jul 22, 2021
The .reduce() method is maybe the most powerful, yet least understood array method. It basically allows us to transform an array into... nearly anything else

Let's re-implement 1๏ธโƒฃ0๏ธโƒฃ lodash functions to learn more about how for examples on how .reduce works

/thread ๐Ÿ‘‡๐Ÿพ๐Ÿงต
1๏ธโƒฃ sum()

โ„น๏ธ Computes the sum of the values in an array

The function is called a "reducer" & the 2nd param is our initial value

The 1st arg of the reducer is the "accumulator" (the value we're building up). The 2nd is the current array element in the iteration

/thread ๐Ÿ‘‡๐Ÿพ๐Ÿงต
2๏ธโƒฃ countBy()

โ„น๏ธ Creates an object w/ keys that are the array elements and values that are their counts

Here we're turning an array into an object

(can't forget to return the object we're accumulating!)

/thread ๐Ÿ‘‡๐Ÿพ๐Ÿงต
Read 13 tweets
Jun 1, 2021
React component patterns help us create reusable and extendable components that provide some inversion of control to their parent

Here's a thread of 5๏ธโƒฃ such patterns

But first let's start with a vanilla <Button> component with "normal" props & how it's used

/thread ๐Ÿงต๐Ÿ‘‡๐Ÿพ ImageImage
1๏ธโƒฃ Placeholder props

The placeholder props pattern allows the shared component to control its layout & any logic, but give some control to the parent for the display

For our Button comp, the `startIcon` & `endIcon` props can be <svg>, <img> or even other React components

๐Ÿงต๐Ÿ‘‡๐Ÿพ ImageImage
2๏ธโƒฃ Polymorphic components

The polymorphic component pattern comes in handy when we need flexibility on the rendered element

For semantic HTML or a11y reasons we may need to change the root element. We can even pass another component for the `as` prop (react-router <Link>)

๐Ÿงต๐Ÿ‘‡๐Ÿพ ImageImageImage
Read 8 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(