An example about using Redux in React Native
PART 1 (Basics): https://www.youtube.com/watch?v=VwrJTgr2UCU
PART 2 (Combine reducers): https://www.youtube.com/watch?v=WGRE0csevfE
PART 3 (Redux thunk): https://www.youtube.com/watch?v=QKVYP7PQ_pI
=> maybe some child components don't need the passing data, but it contains a component that needs this data) => data is useless in some components
- Redux makes it's easy to test (actions tell us what is happening) => traditional state in every components makes us crazy when something wrong but we don't know where it happened
THREE OBJECTS IN REDUX: STORE, REDUCER AND ACTION
npm i --save redux react-reduxCreate a simple store => store is used to contain your app states (combines all components state)
Create a simple reducer => reducer is a pure function which returns new state based on an action (I will explain this in later steps). Reducer has 2 parameters:
- state: this is state of current reducer
- action: simple is a plain object contains some value, such as: type (often a string tell something about this action), payload (contains data),...
=> Because reducer is a pure function, we shouldn't change these parameters => this is a rule :)))
=> NOTE: reducer 'contains' state (we init this with 'initialState')
Connect REDUX to our app
Build REDUX FRAMEWORK => create folders: store, reducers, actions
Combine reducers
Use middleware redux-thunk => used for actions which don't return a plain object, such as fetch data from API
npm i --save redux-thunk