Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Guide to React hook- useReducer
Srishti Prasad
Srishti Prasad

Posted on

     

Guide to React hook- useReducer

What is useReducer Hook?

useState hook is not the only hook to manage state, useReducer is also used for the same. UseReducer just provides us with a mechanism to change a state based on the rules we provide, taking an initial state as input. That is simply useState dressed up.
useReducer hook is better than useState hook when we want to manage complex component states.

Syntax of useReducer and its comparison with useState syntax

Syntax which we have been using for useState() is:
const [ state, setState ] = useState( initialState )

For useReducer() we’ll be using
const [ state, dispatch ] = useReducer( reducer, initialState )

The useReducer hook takes three arguments including reducer, initial state, and the function to load the initial state.
Herereducer is the user-defined function that pairs the current state with thedispatch method to handle the state.

Since comparison helps to learn and estimate better, So useReducer will be more clear to you as I will be explaining by comparing it with useState hook.

From useState to useReducer

Since useReducer is the best solution in React for handling complex state interactions so let's look at how we can convert a component from useState to useReducer.

import{useState}from"react";functionApp(){const[count,setCount]=useState(0);functionincrement(){setCount(count+1);}functiondecrement(){setCount(count-1);}return(<divclassName="App"><h1>Counter Value :{count}</h1><buttononClick={increment}>increase</button><buttononClick={decrement}>decrease</button></div>);}exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

In the above code we have a very simple counter component which can increment, decrement. In order to start converting this to use the useReducer hook we first need to remove the useState call and replace it with useReducer, but before we can do that we need to understand how useReducer is called.

UseReducer works similarly to useState in that it accepts a starting state as one of its arguments and provides us with the current state as well as a method for updating that state. Similar to useState, useReducer also re-renders a component when the state changes. The only significant distinction is that in order to modify our state, we must also give a reducer function to useReducer which we don't do in useState.

const [count, dispatch] = useReducer(reducer, 0)
In the above code you can see that the default state of 0 is passed as the second argument to useReducer and the count is returned as the first element in the array just like with useState. Now instead of having a setCount function we have a dispatch function which allows us to call the reducer function we pass to useReducer.

functionreducer(state,action){switch(action.type){case'increment':returnstate+1casedecrement:returnstate+1Default:returnstate}}const[state,dispatch]=useReducer(reducer,initialState)
Enter fullscreen modeExit fullscreen mode

We now have defined the reducer function and it takes two parameters. The first parameter is the current state of our component. In our case this is just our count. The second parameter is our action which is going to be set to whatever you pass to dispatch. I will cover this more in just a bit. Now inside of the reducer function we have a set of defined actions we can perform on our state. In our case the only action we can perform is the increment action, so if we pass { type: 'increment } to dispatch then it will increase our count by one, otherwise the count will not change.

Modified code using useReducer()

import{useReducer}from"react";constinitialState=0;functionreducer(state,action){switch(action.type){case"increment":returnstate+1;case"decrement":returnstate-1;default:thrownewError();}}functionApp(){const[state,dispatch]=useReducer(reducer,initialState);return(<divclassName="App"><h1>Counter Value :{state}</h1><buttononClick={()=>{dispatch({type:"increment"})}}>increase</button><buttononClick={()=>{dispatch({type:"decrement"})}}>decrease</button></div>);}exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

*When we click on increase button we want to dispatch an action *
But what happens if you want to provide your reducer some data? It's actually quite easy to do this. We may just add our data to the object we send to dispatch because we are free to pass anything we want to dispatch. The common practice is to put all your data inside a property called payload on your object. Here's an illustration of how to achieve it.

import{useReducer}from"react";constinitialState=0;functionreducer(state,action){switch(action.type){case"increment":returnstate+1;case"decrement":returnstate-1;case"change-state":returnstate+action.payload.amount;case"reset":return0;default:returnstate;}}functionApp(){const[state,dispatch]=useReducer(reducer,initialState);return(<divclassName="App"><h1>Counter Value :{state}</h1><buttononClick={()=>{dispatch({type:"increment"});}}>       increase</button><buttononClick={()=>{dispatch({type:"decrement"});}}>       decrease</button><buttononClick={()=>dispatch({type:"reset"})}> Reset</button><buttononClick={()=>{dispatch({type:"change-state",payload:{amount:5}});}}>       Add 5</button></div>);}exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

Simply adding a new section to our reducer to handle this new action was all that was required to add this new action. Then we added a call to dispatch to initiate that operation, and we provided it with a payload containing the quantity by which we wish to alter our state.

CONCLUSION

Simple state can be built up inside of a component using the useState function. However, it is usually best to switch to useReducer when state begins to become more complicated and is shared by several components since useReducer makes it simpler to create sophisticated state interactions without producing a huge complex mess of code.

Complete code is available here :https://codesandbox.io/s/agitated-antonelli-dezsfz?file=/src/App.js

If you have any questions, leave a comment and I'll do my best to respond.
Give this blog a like ❤️ if you found it helpful and follow me for more blogs like this.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
himanshupal0001 profile image
Himanshupal0001
Full stack web Dev. Like to teach and learn. Open for free lance and open source projects.
  • Email
  • Location
    DXB
  • Education
    Chandigarh University
  • Work
    Full stack developer
  • Joined

So I'm learning about redux today and I can't see much diffrenece between them. I consider useReducer is disguised as redux.

CollapseExpand
 
srishtikprasad profile image
Srishti Prasad
Javascript || Node Js || Express || mongoDB || DBMS || NoSQL || C++
  • Email
  • Location
    India
  • Education
    BTech @ National Institute of Technology Bhopal, India
  • Work
    Software development Engineer
  • Joined

Yaa@himanshupal0001 , the only difference which I see is that redux creates one global state container which is above your whole application whereas useReducer creates an independent component within your component itself.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Javascript || Node Js || Express || mongoDB || DBMS || NoSQL || C++
  • Location
    India
  • Education
    BTech @ National Institute of Technology Bhopal, India
  • Work
    Software development Engineer
  • Joined

More fromSrishti Prasad

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp