3 ms·
You're learning how to build them, not just how to use them. The selling point of react hooks is that they're much easier to compose than class methods. The id
by iudqnolq 4y ago
You're learning how to build them, not just how to use them.
The selling point of react hooks is that they're much easier to compose than class methods. The idea is you can easily build your own custom ones that are thin wrappers around one or more of the builtin ones.
This course is presumably walking you through building 50 high-quality hooks so that you're prepared to build your own. For example, one of the 50 is useFirestoreQuery. Understanding how to build that presumably prepares you to learn how to write useMyCorpNotificationMechanism.
A lot of their 50 are trivial but handy. For example
const useToggle = (initialState = false) => {
const [state, setState] = useState(initialState);
const toggle = useCallback(() => setState(state => !state), []);
return [state, toggle]
}
- ricardobeat 4y agoDon't forget the mandatory destructuring on the other side const [state, toggle] = useToggle() otherwise you'll end up causing unnecessary re-renders. Happens too often on hooks returning objects. You can also memoize the whole hook. I miss classes sometimes.