Redux
HOW TO INSTALL REDUX
IMPORT REDUX
TOP 3 CORE CONCEPTS
Store: save the application state
Action: describe what happend on state The only way to interact with the store
Reducer: applay the action on the store state
TOP 3 MAIN PRINCIPLES
- store all application state in single object in the store
- you can't modifies the store state only if you send an action to redux
- you need to write reducer to determine how store state change when an action dispatched
ACTIONS
Action: is a JSON object with type
we prefer to make the type as constance to avoid wrong spilling
Action Builder: is a function that returns an specific action so when you edit an action you just need to edit the builder.
REDUCER
reducer: is a function that accept prevState and an action then return the new state
at first we pass the initial state to reducer
STORE
Store: contains the application state
HOW TO CREATE A STORE
STORE MAIN FUNCTIONS
- getState() ➡ is a function that returns the current state
- subscribe(subscriber) ➡ is a function that accept another function then the redux will call this function automatically when the store state is updated like Observer Pattern
- unsubscribe() ➡ is a function return from calling subscribe function, when unsubscribe function it will remove the subscriber from the redux update list
- dispatch(action) ➡ is a function that recive an action and pass it to the reducer function
full store example
Output
MULTIBLE REDUCERS
sometimes you need to create more than one reducer each on to handle different state
Example state for cake and state for icecream
but createStore function does not accept more than one reducer so redux give us a function to compine reducers