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 🤮)
🧵👇🏾
On the other hand, interfaces are good for extending an object type when we wanna override a property
When we use type intersections, the property types are intersected, usually resulting in a `never` type. We _can_ 1st use `Omit<>` to achieve "overriding" w/ type aliase
🧵👇🏾
Based on the guidance from the TS docs, I still default to interfaces & use type aliases for "type expressions"
But at least now I have a more concrete explanation for interfaces vs type aliases in TypeScript
Relatedly, I avoid using `any` in TypeScript, opting to be as strict as possible. But some functions really don't (need to) know what the type of a parameter is
Check out this post from early this year explaining when to use `unknown` vs `any`
If you wanna learn more about TypeScript, DivOps, React, or other web frontend tech, feel free to subscribe to the weekly BenMVP Newsletter. You won't regret it!*
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()`
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!)
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