In my hooks talk for the Cleveland React Meetup, I live-coded a conversion from a class-based app to one using hooks.
This repo has branches containing each step along the way, from the basic class-based app to the completed thing.
I created Pull Requests detailing each step, to make it easy to see diffs along the way. Each pull request has notes on the specific steps taken:
- Step 1 - Convert Filter component
- Step 2 - Convert SelectUsers component
- Step 3 - Create useLoad custom hook and integrate into App component
- Step 4 - Convert Todos component
- Bonus Step 5 - Move api client into Context
- Bonus Step 6 - Create a UserFilter Context and Custom Hook
(master and the final branch are identical)
Feel free to leave comments or questions in issues or the PRs - would be happy to discuss anything!
Here are some resources I've found helpful:
- An invaluable resource, expecially Hooks Reference and Hooks FAQs
- A fantastic tool to help with following the rules of hooks, and to help manage dependency arrays.
-
A small package to help development - this overwrites
useEffect
to throw an error when an effect has run too many times at once. Prevents crashing your browser when you've messed up an effect dependency and created an infinite loop. -
A super-deep dive into
useEffect
; really helped me understand how to deal with effect dependencies and their relationship with the render. - A great resource for anyone writing components, but especially if you are beginning to use hooks.
- A seminal article on doing data fetching with Hooks. Robin runs through a number of different approaches and use-cases for fetching data.
This project was bootstrapped with Create React App.
To install, clone the repo, then run yarn install
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
Builds the app for production to the build
folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed!
See the section about deployment for more information.
You can learn more in the Create React App documentation.
To learn React, check out the React documentation.