Redux
1. State Management
Global State Management
State in frontend development: Data expressed dynamically
Global State
If different components use different types of state, it's okay to have different sources
However, if different components handle the same state, it's better to avoid getting it from different sources
Data Integrity
Identical data is always retrieved from the same place β Single source of truth
Problems State Management Tools Solve
Provide global state storage
Solve Props Drilling issues
Props Drilling
Phenomenon of passing data through components that are only used to pass props
To pass state from a parent component to a component that wants to receive it through props
Problems with Props Drilling
Code readability becomes poor
Code maintenance also becomes difficult
When state changes, components unnecessarily involved in the props passing process also re-render, negatively affecting performance
Solutions for This
Keep state related to components as close as possible
Use state management libraries: Effective in preventing Props Drilling since state can be directly retrieved from globally managed storage
2. What is Redux
A state management library that separates components and state to enable global state management
Order of Redux State Management
When an event that requires state change occurs, an Action object containing information about the state to be changed is created
This Action object is passed as an argument to the Dispatch function
The Dispatch function passes the Action object to the Reducer function
The Reducer function checks the Action object's value and changes the state of the global state storage Store according to that value
When the state changes, React re-renders the screen
In other words, in Redux, data flows unidirectionally in the order Action β Dispatch β Reducer β Store
3. Redux Structure
Store
Acts as the only storage where state is managed, the space where the Redux app's state is stored
Reducer
A function that changes state according to the type value of the Action object received from Dispatch
Action
Action is an object that defines what action to take, type must be specified as required, and is written in uppercase and Snake Case. Usually, a function that creates Action objects is made and used, which is called an Action Creator.
Dispatch
A function that passes Action to Reducer
4. Redux Hooks
useDispatch()
A method that returns the Dispatch function that passes Action objects to Reducer
useSelector()
A method that connects components and state to access Redux's state
5. Three Principles of Redux
Single source of truth
Identical data must always be retrieved from the same place
Redux has only one space called Store for storing data
State is read-only
State is read-only
Just like in React where state was only changed with state update functions, Redux state cannot be changed directly
In other words, state can only be changed when there's an Action object
Changes are made with pure functions
Changes are only possible with pure functions
Must be written only with pure functions to prevent state from being changed to unexpected values
Last updated