Unlocking Redux: A Comprehensive Guide for React Developers
Ziran Fuzooly
Senior Software Engineer at Insharp Tech| Mentor | Proficient in ReactJS, NextJS, JavaScript, TypeScript, Java & SpringBoot | Passionate about FullStack Development | Collaborative Problem Solver | Continuous Learner
Redux can be confusing for beginner React developers to understand. You need to know many concepts to use appropriately, like reducers, actions, store, pure functions, immutability, and much more.
But every React developer should know how to work with Redux since industry projects often use Redux to manage larger projects.
What is Redux?
“Redux is a predictable state container for JavaScript apps.”
Redux is a state management library that helps you better manage (global) state in your applications.
Redux is a pattern and library for managing and updating application state, using events called “actions”. It serves as a centralised store for state that needs to be used across your entire application, with rules ensuring that the state can only be updated in a predictable fashion.
Three Core Principles of Redux
Store :- A Store is a place where the entire state of your application lists. It manages the status of the application and has a dispatch(action) function. It is like a brain responsible for all moving parts in Redux.
conslole.log("store.getState()");
2. dispatch() dispatches an action. It is the only way to update the application state
Buttonchange: () => dispatch({msg:"Message_change"})
3. Subscribe() subscribes a change listener to the state. unsubscribe() is useful when you no linger want to call your listener method when the state changes.
const unsubscribe = store.subscribe(handleChange)
unsubscribe()
Action :- Action is sent or dispatched from the view, which are payloads that Reducers can read. It is a pure object created to store the information of the user’s event. It includes information such as type of action, time of occurrence, location of occurrence, its coordinates, and which state it aims to change.
Reducer :- Reducer reads the payloads from the actions and then updates the store via the state accordingly. It is a pure function to return a new state from the initial state.
领英推荐
Why use Redux?
3. It encourages good ‘React’ architecture.
The patterns and tools provided by Redux make it easier to understand when, where, why, and how the state in your application is being updated, and how your application logic will behave when those changes occur. Redux guides you towards writing code that is predictable and testable, which helps give you confidence that your application will work as expected.
Redux Libraries & Tools.
Pros and Cons of Redux
Summary
Redux is a library for managing global application state
Redux uses a “one-way data flow” app structure
The UI dispatches an action
The store runs the reducers, and the state is updated based on what occurred
The store notifies the UI that the state has changed
Redux uses several types of code