From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

useReducer vs Redux: Summary

useReducer vs Redux: Summary

Welcome back, let's understand what is lazy initialization using the init function with our useReducer hook. Our useReducer hook can have another parameter other than the reducer and the initial state which is the initialization function. At times we have cases when our initial state computation is pretty expensive. And for that purpose we need to provide a lazy initialization hook similarly like we have in the use state as well. So that that particular computation can happen only once. The init function gets to call only once for the initial render for the very first time. So you can have your initialization function which can be init or anything else you want to call it. This initialization function will do the expensive computation and you can pass on this function after your reducer, the initial state and then your initialization function. This helps us to do the expensive computation only once. Let's quickly do a SWOT analysis or a comparison of useReducer hook versus Redux. So…

Contents