#Christ follower, husband, father of 3οΈβ£. Tweetin' on #React #TypeScript #DivOps @NBA. π Speaker @GoogleDevExpert @MVPAward. Principal FE Eng @stitchfix
Dec 23, 2021 β’ 8 tweets β’ 4 min read
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
π§΅ππΎ
Nov 18, 2021 β’ 7 tweets β’ 5 min read
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
π§΅ππΎ
Oct 28, 2021 β’ 9 tweets β’ 5 min read
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!
π§΅ππΎ
Oct 13, 2021 β’ 8 tweets β’ 4 min read
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)
π§΅ππΎ
Jul 22, 2021 β’ 13 tweets β’ 5 min read
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 ππΎπ§΅
Jun 1, 2021 β’ 8 tweets β’ 4 min read
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 π§΅ππΎ
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
π§΅ππΎ
May 19, 2021 β’ 7 tweets β’ 4 min read
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
π§΅ππΎ
Mar 23, 2021 β’ 6 tweets β’ 3 min read
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
π§΅ππΎ
Jan 26, 2021 β’ 8 tweets β’ 4 min read
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
π§΅ππΎ
Jan 14, 2021 β’ 7 tweets β’ 4 min read
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...
π§΅ππΎ
Jan 5, 2021 β’ 6 tweets β’ 3 min read
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 π§΅ππΎ
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
π§΅ππΎ
Dec 15, 2020 β’ 12 tweets β’ 6 min read
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 π€