SlideShare a Scribd company logo
1 of 23
ReduxJS
Kavit Shah
Topics
What is redux ?
Redux Principles
Data Flow in Redux
Understanding Action, Reducers, Store
Data flow in React-Redux App
Demo
Redux Utilities
React philosophy
UI is most predictable when it’s the pure function of state. React solves this problem.
Redux is the data/state management library.
Redux attempts to make state mutations predictable by imposing certain restrictions on how and when state updates can
happen.
UI = f (state)
What is Redux?
What is redux?
On the Front end you have to manage states mutations happening either synchronously or asynchronously.
States management includes managing
Server side data
Locally created data which is not persisted on the server
Cached data
the active route, the selected tab, whether to show a spinner or not, should pagination controls be displayed etc…
Mixing state mutations and asynchronicity is like mixing mentos and coke.
Redux Principles
1. Single source of truth - The state of your whole application is stored in an object tree within a single
STORE.
1. State is read-only - The only way to mutate the state is to dispatch an action, an object describing what
happened called ACTION.
1. Changes are made with pure functions - To specify how the state tree is transformed by actions, you
write pure REDUCER.
Data Flow In Redux
State Tree For Simple Todo List
{
visibilityFilter: 'SHOW_ALL',
todos: [
{
id: 1,
text: 'Lets make G2M Awesome',
completed: false
},
...
]
}
Mapping between state tree and reducers
Data Flow In Redux
Actions
Actions is information that send data from your application to your store.
They are the only source of information for the store.
You send them to the store using store.dispatch().
Actions must have a type property that indicates the type of action being performed.
Flux-standard-action is one approach on how to construct your actions.
For example,
{
type: 'TOGGLE_TODO',
index: 5
}
{
type: 'ADD_TODO',
text: 'Complete Code Review'
}
{
type: 'SET_VISIBILITY_FILTER',
filter: 'SHOW_COMPLETED'
}
Action Creator
dispatch(addTodo(text))
Synchronous Action Creator Asynchronous Action Creator
function addTodo(text) {
return {
type: ADD_TODO,
text
}
}
function addTodo(text) {
return ajax.postJSON({
type: ADD_TODO,
text
});
}
Data Flow In Redux
Reducers
Reducers specify how the application’s state changes in response to dispatched action.
Reducer is a function which has the following signature :
(previousState, action) => newState
Reducer Example
function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return {
...state,
visibilityFilter: action.filter
}
case ADD_TODO:
return {
state, {
todos: [
...state.todos,
{
text: action.text,
completed: false
}
]
})
default:
return state
}
}
Things not to do inside reducers.
● Mutate the state.
● Perform side effects like API calls and routing
transitions;
● Call impure functions, e.g. Date.now() or
Math.random().
Note:
We always return the previous state for default case.
Data Flow In Redux
Store
The Store is the object that actions and reducers together. The store has the following responsibilities:
Holds application state;
Allows access to state via getState();
Allows state to be updated via dispatch(action);
Registers listeners via subscribe(listener);
Handles unregistering of listeners via the function returned by subscribe(listener).
Redux store implementation
Store
Store
Reducer
{
…
contacts: {}
}
Data Flow In Redux
Data Flow in React-Redux
Presentational component
( Render using updated state )
Container component
store.dispatch(action())
Reducers
(currentState, action) => nextState
State tree
Event handler
Event emitter
Demo
Simple redux counter without UI
Simple react-redux counter with UI
Demo II
Simple redux todo app without UI
Simple react-redux todo app with UI version 1
Simple react-redux todo app with UI, with separate presentational and container
components
Utilities
Name of utility Name of the function Use case of the function
redux-actions ● createAction To create action object with given type and the
payload
react-redux ● Provider
● Connect
Provider - passes store via context
Connect - generates container components
from presentational components
redux-create-reducer ● createReducer Creats a mapping between reducer and event
type. Hence, you don’t have to manage the
mapping
redux ● combineReducers Generates a root reducer from the given set of
reducers
Learning resources
● ReduxJS
● [video] Getting started with redux
● [video] React with redux

More Related Content

What's hot

What's hot (20)

ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
React js t2 - jsx
React js   t2 - jsxReact js   t2 - jsx
React js t2 - jsx
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentation
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
 
Redux training
Redux trainingRedux training
Redux training
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
ReactJS
ReactJSReactJS
ReactJS
 

Similar to Getting started with Redux js

React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with ReduxVedran Blaženka
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тягаVitebsk Miniq
 
Damian Kmiecik - Road trip with Redux
Damian Kmiecik - Road trip with ReduxDamian Kmiecik - Road trip with Redux
Damian Kmiecik - Road trip with ReduxPROIDEA
 
How to use redux with react hooks in react native application
How to use redux with react hooks in react native applicationHow to use redux with react hooks in react native application
How to use redux with react hooks in react native applicationKaty Slemon
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptxNavneetKumar111924
 
[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux ArchitectureNaukri.com
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingBinary Studio
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and reduxCuong Ho
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxVisual Engineering
 
Redux State Management System A Comprehensive Review
Redux State Management System A Comprehensive ReviewRedux State Management System A Comprehensive Review
Redux State Management System A Comprehensive Reviewijtsrd
 
Maintaining sanity in a large redux app
Maintaining sanity in a large redux appMaintaining sanity in a large redux app
Maintaining sanity in a large redux appNitish Kumar
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Katy Slemon
 
Manage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxManage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxCommit University
 
iOS Architectures
iOS ArchitecturesiOS Architectures
iOS ArchitecturesHung Hoang
 

Similar to Getting started with Redux js (20)

React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тяга
 
Redux
ReduxRedux
Redux
 
Reducers+flux=redux
Reducers+flux=reduxReducers+flux=redux
Reducers+flux=redux
 
Damian Kmiecik - Road trip with Redux
Damian Kmiecik - Road trip with ReduxDamian Kmiecik - Road trip with Redux
Damian Kmiecik - Road trip with Redux
 
How to use redux with react hooks in react native application
How to use redux with react hooks in react native applicationHow to use redux with react hooks in react native application
How to use redux with react hooks in react native application
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
React
ReactReact
React
 
Redux State Management System A Comprehensive Review
Redux State Management System A Comprehensive ReviewRedux State Management System A Comprehensive Review
Redux State Management System A Comprehensive Review
 
Maintaining sanity in a large redux app
Maintaining sanity in a large redux appMaintaining sanity in a large redux app
Maintaining sanity in a large redux app
 
Redux Tech Talk
Redux Tech TalkRedux Tech Talk
Redux Tech Talk
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...
 
Manage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxManage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's Redux
 
React/Redux
React/ReduxReact/Redux
React/Redux
 
iOS Architectures
iOS ArchitecturesiOS Architectures
iOS Architectures
 

Recently uploaded

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 

Recently uploaded (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 

Getting started with Redux js

  • 2. Topics What is redux ? Redux Principles Data Flow in Redux Understanding Action, Reducers, Store Data flow in React-Redux App Demo Redux Utilities
  • 3. React philosophy UI is most predictable when it’s the pure function of state. React solves this problem. Redux is the data/state management library. Redux attempts to make state mutations predictable by imposing certain restrictions on how and when state updates can happen. UI = f (state) What is Redux?
  • 4. What is redux? On the Front end you have to manage states mutations happening either synchronously or asynchronously. States management includes managing Server side data Locally created data which is not persisted on the server Cached data the active route, the selected tab, whether to show a spinner or not, should pagination controls be displayed etc…
  • 5. Mixing state mutations and asynchronicity is like mixing mentos and coke.
  • 6. Redux Principles 1. Single source of truth - The state of your whole application is stored in an object tree within a single STORE. 1. State is read-only - The only way to mutate the state is to dispatch an action, an object describing what happened called ACTION. 1. Changes are made with pure functions - To specify how the state tree is transformed by actions, you write pure REDUCER.
  • 7. Data Flow In Redux
  • 8. State Tree For Simple Todo List { visibilityFilter: 'SHOW_ALL', todos: [ { id: 1, text: 'Lets make G2M Awesome', completed: false }, ... ] } Mapping between state tree and reducers
  • 9. Data Flow In Redux
  • 10. Actions Actions is information that send data from your application to your store. They are the only source of information for the store. You send them to the store using store.dispatch(). Actions must have a type property that indicates the type of action being performed. Flux-standard-action is one approach on how to construct your actions. For example, { type: 'TOGGLE_TODO', index: 5 } { type: 'ADD_TODO', text: 'Complete Code Review' } { type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_COMPLETED' }
  • 11. Action Creator dispatch(addTodo(text)) Synchronous Action Creator Asynchronous Action Creator function addTodo(text) { return { type: ADD_TODO, text } } function addTodo(text) { return ajax.postJSON({ type: ADD_TODO, text }); }
  • 12. Data Flow In Redux
  • 13. Reducers Reducers specify how the application’s state changes in response to dispatched action. Reducer is a function which has the following signature : (previousState, action) => newState
  • 14. Reducer Example function todoApp(state = initialState, action) { switch (action.type) { case SET_VISIBILITY_FILTER: return { ...state, visibilityFilter: action.filter } case ADD_TODO: return { state, { todos: [ ...state.todos, { text: action.text, completed: false } ] }) default: return state } } Things not to do inside reducers. ● Mutate the state. ● Perform side effects like API calls and routing transitions; ● Call impure functions, e.g. Date.now() or Math.random(). Note: We always return the previous state for default case.
  • 15. Data Flow In Redux
  • 16. Store The Store is the object that actions and reducers together. The store has the following responsibilities: Holds application state; Allows access to state via getState(); Allows state to be updated via dispatch(action); Registers listeners via subscribe(listener); Handles unregistering of listeners via the function returned by subscribe(listener). Redux store implementation
  • 18. Data Flow In Redux
  • 19. Data Flow in React-Redux Presentational component ( Render using updated state ) Container component store.dispatch(action()) Reducers (currentState, action) => nextState State tree Event handler Event emitter
  • 20. Demo Simple redux counter without UI Simple react-redux counter with UI
  • 21. Demo II Simple redux todo app without UI Simple react-redux todo app with UI version 1 Simple react-redux todo app with UI, with separate presentational and container components
  • 22. Utilities Name of utility Name of the function Use case of the function redux-actions ● createAction To create action object with given type and the payload react-redux ● Provider ● Connect Provider - passes store via context Connect - generates container components from presentational components redux-create-reducer ● createReducer Creats a mapping between reducer and event type. Hence, you don’t have to manage the mapping redux ● combineReducers Generates a root reducer from the given set of reducers
  • 23. Learning resources ● ReduxJS ● [video] Getting started with redux ● [video] React with redux

Editor's Notes

  1. Changes in the state is explicit. Hence, it’s possible to keep track of all of them. You can see the history of the state changes and that allows you to do time travel debugging. This approach scales well for medium and complex app. The UI does not need to know how to update the state. All they need to know is which action to dispatch. State mutations happen only using the pure reducers. Pure meaning it can not modify the arguments which are passed to it. It does not do any network calls. If you call the function with the same arguments it always returns the same value. Simple example of impure function is Math.random() or Date().
  2. One way data flow between different components.