Actions are plain JavaScript objects describing what happened.
Structure of an action:
type → required, describes the action
payload → optional, contains data for the reducer
Example Actions:
Reducers
Reducers are pure functions that take the current state and an action, and return new state.
Rules for reducers:
Pure function (no side effects)
Returns a new state object, does not mutate the existing state
Handles different action types
Example Counter Reducer:
Example: Counter App with Redux
Step 1: Install Redux
Step 2: Create Store and Reducer
Step 3: Connect Redux to React
How it works:
store holds the state
dispatch sends actions to the reducer
useSelector reads state from the store
useDispatch sends actions to update state
Best Practices
Keep reducers pure and small
Use action creators for consistency
Split large state into multiple reducers using combineReducers
Use Redux DevTools for debugging state changes
Avoid storing derived data in the store; compute it in selectors