
Writing My First Redux Code — And Realizing How Similar It Is to useReducer
Yesterday I started exploring Redux and one thing immediately stood out to me. Redux feels very similar to React’s useReducer hook . Yesterday I mostly observed and tried to understand the structure. But today I finally started writing my own Redux code , and the similarities became even clearer. Step 1: Creating the Initial State Just like useReducer , the first thing we need in Redux is an initial state . This initial state holds the starting values for our application. For example, it might contain things like: account balance loan amount transaction history All the default values are defined here before any action changes the state. Step 2: Writing the Reducer Function The next step is creating a reducer function . This is also very similar to useReducer . The reducer receives two things: state action Inside the reducer we usually use a switch statement to handle different action types. Each case checks the action type , and if needed it can also use the payload to update the state
Continue reading on Dev.to React
Opens in a new tab

