Skip to content

jleonardo007/custom-hooks-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started with Create React App

This project was bootstrapped with Create React App.

Available Scripts

In the project directory, you can run:

yarn start

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.

yarn test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

yarn build

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.

In React, custom hooks have the purpose to group the logic related with the state and effects (useState,useEffect and another hooks) so this way the rest of the component (mostly jsx) consumes the data bring by the custom hook. Today, we take a look on this approach by implementing a timer component.

Our component looks like this:

Timer Component

This component is compose by two components more, a <TimerDisplay/> (blue box) and a <TimerControls/> (orange box)

Timer Components

Now, take a detailed look to their respectives codes:

<App/> code looks like this.

app code

You notice both the state-effects logic and the jsx are within the component <App/> this is ok but think a moment if our Timer component requires more features is quite likely that state-effects logic grows and ofcourse the jsx too and yes, this becomes in a code hard to read, maintain and scale. And that's not all, make zoom at the return statement:

return statement

Like you see, the <TimerControls/> has the prop setTimer, and means this uses the state update function directly.

Timer controls code

Dont be scared, it's just a simple component with a few handlers in it, but yes, you guessed it, if the parent component grows <TimerControls/> will too.

So the solution is separate the state-effects logic and handlers and implement them througth a custom hook. In this case, our custom hook gonna be useTimer(). Is mandatory add the word use before the hook name this way React knows that the component uses a hook.

useTimer() code looks like this.

use timer code

In this case useTimer() imports the handlers cause each one requires the setTimer() (if you have a handler that doesn't update the state, the handlers can be consume by the component itself and not the custom hook). The new handlers code looks like this.

handlers code

The one million question is how <App/> consumes useTimer()? Make zoom again but now at the useTimer() return statement:

use timer return

useTimer() returns and object with timer (the state), alarmRef (it's just a ref attatched to an <audio> tag that plays when timer comes to zero) and the handlers (setMinutes , playOrPauseTimer and resetTimer). About the last ones note that are functions that returns another functions (the handlers imported) aka closures, now see how the components look like:

<App/>

App component

<TimerControls/>

timer controls

Conclusions

  • If you think that your components code gonna grow, separate the state-effects logic and handlers througth a custom hook.
  • If your components handlers require update the state use them within a custom hook.
  • Don't forget the use word before name your hook.
  • Some React experts think that React more that an UI library is a mental model, so the most important hook that you can use is useYourImagination

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published