SlideShare a Scribd company logo
An Introduction to
Redux
Amin Ashtiani
Why you should use
redux?
1. React was designed to create UI component and each
component internally manage their state without any need
for an external library or tool and it’s a problem when our
application grow bigger
2. About shared state among component, it’s hard to detect
where is state live
3. When a shared state changed , it should lifted up to
nearest parent component which is common between the
other components and then pass down to the other child
What is Redux?
Redux is a predictable state container for JavaScript applications. It helps you write applications that
behave consistently, run in different environments (client, server, and native), and are easy to test.—
https://redux.js.org/
➔ Predictable state updates
entire state of an application is stored in one central location.
➔ "pure" reducer functions
makes it easier to implement things like logging changes to the data,
or persisting data between page refreshes
➔ Centralizing the state
makes it easier to implement things like logging changes to the data,
or persisting data between page refreshes
Misconception
There is a common misconception that
Redux was made to work with React and
just that but as you see in definition of
redux , it can use in client, server and
native.
Redux is a state container for any vanilla
JavaScript application.
How it is
working?
The way Redux works is simple. There is a
central store that holds the entire state of the
application. Each component can access the
stored state without having to send down
props from one component to another.
How it is working?
There is three building part:
1. Store
2. Action
3. Reducer
Tip
Remember. If something
sounds like common
sense, people will
ignore it.
Highlight what is
unexpected about
your topic.
Action is a plain object like
this:
1. It should have a type property, and you can
send data with payload,
2. Each action created by action creator
3. You should pass this object to
store.dispatch()
Tip
Simply put, actions are
events. User interactions
, Api calls , …
Remember that actions
only describe what
happened.
Action in Use
1st step 2nd step 3th step
Define action types
Types should typically
be defined as string
constants
Define Action Creator
action creators simply return an
action
Dispatch an action
Dispatch your action
whenever needed
Example
Reducers
specify how the application's state
changes in response to actions sent
to the store.
How it is working?
Reducers are pure functions that take:
1. the current state of an application,
2. perform an action
3. returns a new state
Tip
A pure function is a
function which:
1. Given the same input, will
always return the same
output.
2. Produces no side effects.
Side Effects
A side effect is any application state change that
is observable outside the called function other
than its return value.
Side effects are mostly avoided in functional
programming, which makes the effects of a
program much easier to understand, and much
easier to test.
1. Modifying any external variable or object
property (e.g., a global variable, or a
variable in the parent function scope
chain)
2. Logging to the console
3. Writing to the screen
4. Writing to a file
5. Writing to the network
6. Triggering any external process
7. Calling any other functions with side-
effects
Example
Example
Store
A store holds the whole state tree of
your application. The only way to
change the state inside it is to
dispatch an action on it.
A store is not a class. It's just an
object with a few methods on it
Data Flow
1. You dispatch an action
2. The Redux store calls the
reducer function you gave it
3. The root reducer may combine
the output of multiple reducers
into a single state tree
4. The Redux store saves the
complete state tree returned by
the root reducer.
Example
Presentational and Container Components
Simple implementation of container
Example
Example
Example
Features Redux MobX GraphQL Jumpsuit
DRY principle No Yes Yes Yes
Complication High Low Medium Medium
Learning curve High Low Medium Low
Application Suitable for
simple
application
Suitable for
complex
application
Suitable for
medium sized
application
Suitable for
complex
application
Comparison Table of Redux Alternatives
Question:
But how Redux
deals with async
action?
Async Flow
For handling Async action we need a
Middleware.
Asynchronous middleware like redux-
thunk or redux-saga wraps the store's
dispatch() method and allows you to
dispatch something other than
actions, for example, functions or
Promises.
Pros and Cons of Redux-Thunk
● Hard to scale
● No built in way
to handle
advance task
● Action creators
are impure
● Easy to learn
● Everything is
basic
javascript(makes it
easy to integrate)
Pros and Cons of Redux-Saga
● quite confusing
at times
● sagas live
outside of your
other redux
logic
● lot of useful
helper functions
for advance task
● heavy use of
ES6
Pros and Cons of Redux-Observable
● quite confusing
at times
● Esoteric testing
● useful helper
functions for
advance task
● heavy use of
ES6
Pros and Cons of Redux-Loop
● No built in way
to handle
advance task
like debouncing
● Easy to follow
the flow of
action
● Everything
remain pure
Conclusion!
If you want something powerful use Redux-
saga or Redux-Observable
If you want something with small learning
curve use redux-thunk
For small personal projects use Redux-Loop

More Related Content

What's hot

Introduction to React Hooks
Introduction to React HooksIntroduction to React Hooks
Introduction to React Hooks
Felicia O'Garro
 
Reducers+flux=redux
Reducers+flux=reduxReducers+flux=redux
Reducers+flux=redux
Shmulik Chicvashvili
 
Angular redux
Angular reduxAngular redux
Angular redux
Nir Kaufman
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
Will React Hooks replace Redux?
Will React Hooks replace Redux?Will React Hooks replace Redux?
Will React Hooks replace Redux?
Trivikram Kamat
 
React
ReactReact
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
Remo Jansen
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
Piyush Jamwal
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with Redux
Maurice De Beijer [MVP]
 
Redux js
Redux jsRedux js
Redux js
Nils Petersohn
 
State Models for React with Redux
State Models for React with ReduxState Models for React with Redux
State Models for React with Redux
Stephan Schmidt
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
Citrix
 
React
ReactReact
React & ES6 Intro
React & ES6 IntroReact & ES6 Intro
React & ES6 Intro
Yair Aviner
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
Mudasir Qazi
 
Spfx with react redux
Spfx with react reduxSpfx with react redux
Spfx with react redux
Rajesh Kumar
 
Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...
Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...
Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...
Codemotion
 
Getting started with react & redux
Getting started with react & reduxGetting started with react & redux
Getting started with react & redux
Girish Talekar
 
Unidirectional Data Flow Architecture (Redux) in Swift
Unidirectional Data Flow Architecture (Redux) in SwiftUnidirectional Data Flow Architecture (Redux) in Swift
Unidirectional Data Flow Architecture (Redux) in Swift
Seyhun AKYUREK
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
Rahul Shukla
 

What's hot (20)

Introduction to React Hooks
Introduction to React HooksIntroduction to React Hooks
Introduction to React Hooks
 
Reducers+flux=redux
Reducers+flux=reduxReducers+flux=redux
Reducers+flux=redux
 
Angular redux
Angular reduxAngular redux
Angular redux
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
Will React Hooks replace Redux?
Will React Hooks replace Redux?Will React Hooks replace Redux?
Will React Hooks replace Redux?
 
React
ReactReact
React
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with Redux
 
Redux js
Redux jsRedux js
Redux js
 
State Models for React with Redux
State Models for React with ReduxState Models for React with Redux
State Models for React with Redux
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
React
ReactReact
React
 
React & ES6 Intro
React & ES6 IntroReact & ES6 Intro
React & ES6 Intro
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Spfx with react redux
Spfx with react reduxSpfx with react redux
Spfx with react redux
 
Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...
Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...
Fabio Biondi - Front-end data architectures in Angular, Redux & NGRX - Codemo...
 
Getting started with react & redux
Getting started with react & reduxGetting started with react & redux
Getting started with react & redux
 
Unidirectional Data Flow Architecture (Redux) in Swift
Unidirectional Data Flow Architecture (Redux) in SwiftUnidirectional Data Flow Architecture (Redux) in Swift
Unidirectional Data Flow Architecture (Redux) in Swift
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
 

Similar to an Introduction to Redux

Redux
ReduxRedux
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malik
Lama K Banna
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
NavneetKumar111924
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
Hùng Nguyễn Huy
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
Vinícius Ribeiro
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
Mildain Solutions
 
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
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
Presentation1
Presentation1Presentation1
Presentation1
Kshitiz Rimal
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol
 
Introduction to Redux.pptx
Introduction to Redux.pptxIntroduction to Redux.pptx
Introduction to Redux.pptx
MohammadImran322154
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR3
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
valuebound
 
20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download
Mohd Quasim
 
Introduction to Redux (for Angular and React devs)
Introduction to Redux (for Angular and React devs)Introduction to Redux (for Angular and React devs)
Introduction to Redux (for Angular and React devs)
Fabio Biondi
 
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
ijtsrd
 
How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react
MoonTechnolabsPvtLtd
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
SHAIKIRFAN715544
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
SHAIKIRFAN715544
 

Similar to an Introduction to Redux (20)

Redux
ReduxRedux
Redux
 
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malik
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
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...
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
Presentation1
Presentation1Presentation1
Presentation1
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
Introduction to Redux.pptx
Introduction to Redux.pptxIntroduction to Redux.pptx
Introduction to Redux.pptx
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download
 
Introduction to Redux (for Angular and React devs)
Introduction to Redux (for Angular and React devs)Introduction to Redux (for Angular and React devs)
Introduction to Redux (for Angular and React devs)
 
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
 
How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react How to increase the ui performance of apps designed using react
How to increase the ui performance of apps designed using react
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
 

Recently uploaded

LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 

Recently uploaded (20)

LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 

an Introduction to Redux

  • 2. Why you should use redux? 1. React was designed to create UI component and each component internally manage their state without any need for an external library or tool and it’s a problem when our application grow bigger 2. About shared state among component, it’s hard to detect where is state live 3. When a shared state changed , it should lifted up to nearest parent component which is common between the other components and then pass down to the other child
  • 3. What is Redux? Redux is a predictable state container for JavaScript applications. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test.— https://redux.js.org/ ➔ Predictable state updates entire state of an application is stored in one central location. ➔ "pure" reducer functions makes it easier to implement things like logging changes to the data, or persisting data between page refreshes ➔ Centralizing the state makes it easier to implement things like logging changes to the data, or persisting data between page refreshes
  • 4.
  • 5. Misconception There is a common misconception that Redux was made to work with React and just that but as you see in definition of redux , it can use in client, server and native. Redux is a state container for any vanilla JavaScript application.
  • 6. How it is working? The way Redux works is simple. There is a central store that holds the entire state of the application. Each component can access the stored state without having to send down props from one component to another.
  • 7. How it is working? There is three building part: 1. Store 2. Action 3. Reducer Tip Remember. If something sounds like common sense, people will ignore it. Highlight what is unexpected about your topic.
  • 8. Action is a plain object like this: 1. It should have a type property, and you can send data with payload, 2. Each action created by action creator 3. You should pass this object to store.dispatch() Tip Simply put, actions are events. User interactions , Api calls , … Remember that actions only describe what happened.
  • 9. Action in Use 1st step 2nd step 3th step Define action types Types should typically be defined as string constants Define Action Creator action creators simply return an action Dispatch an action Dispatch your action whenever needed
  • 11. Reducers specify how the application's state changes in response to actions sent to the store.
  • 12. How it is working? Reducers are pure functions that take: 1. the current state of an application, 2. perform an action 3. returns a new state Tip A pure function is a function which: 1. Given the same input, will always return the same output. 2. Produces no side effects.
  • 13. Side Effects A side effect is any application state change that is observable outside the called function other than its return value. Side effects are mostly avoided in functional programming, which makes the effects of a program much easier to understand, and much easier to test. 1. Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain) 2. Logging to the console 3. Writing to the screen 4. Writing to a file 5. Writing to the network 6. Triggering any external process 7. Calling any other functions with side- effects
  • 16. Store A store holds the whole state tree of your application. The only way to change the state inside it is to dispatch an action on it. A store is not a class. It's just an object with a few methods on it
  • 17. Data Flow 1. You dispatch an action 2. The Redux store calls the reducer function you gave it 3. The root reducer may combine the output of multiple reducers into a single state tree 4. The Redux store saves the complete state tree returned by the root reducer.
  • 19. Presentational and Container Components Simple implementation of container
  • 23.
  • 24.
  • 25. Features Redux MobX GraphQL Jumpsuit DRY principle No Yes Yes Yes Complication High Low Medium Medium Learning curve High Low Medium Low Application Suitable for simple application Suitable for complex application Suitable for medium sized application Suitable for complex application Comparison Table of Redux Alternatives
  • 26. Question: But how Redux deals with async action?
  • 27. Async Flow For handling Async action we need a Middleware. Asynchronous middleware like redux- thunk or redux-saga wraps the store's dispatch() method and allows you to dispatch something other than actions, for example, functions or Promises.
  • 28.
  • 29. Pros and Cons of Redux-Thunk ● Hard to scale ● No built in way to handle advance task ● Action creators are impure ● Easy to learn ● Everything is basic javascript(makes it easy to integrate)
  • 30. Pros and Cons of Redux-Saga ● quite confusing at times ● sagas live outside of your other redux logic ● lot of useful helper functions for advance task ● heavy use of ES6
  • 31. Pros and Cons of Redux-Observable ● quite confusing at times ● Esoteric testing ● useful helper functions for advance task ● heavy use of ES6
  • 32. Pros and Cons of Redux-Loop ● No built in way to handle advance task like debouncing ● Easy to follow the flow of action ● Everything remain pure
  • 33. Conclusion! If you want something powerful use Redux- saga or Redux-Observable If you want something with small learning curve use redux-thunk For small personal projects use Redux-Loop

Editor's Notes

  1. Sometimes we can avoid side-effects. Imagine when you want to insert a data into database.
  2. We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead. We return the previous state in the default case. It's important to return the previous state for any unknown action.
  3. We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead. We return the previous state in the default case. It's important to return the previous state for any unknown action.
  4. 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). It has 4 method: getState : Returns the current state tree of your application. It is equal to the last value returned by the store's reducer. Dispatch : Dispatches an action. This is the only way to trigger a state change. Subscribe : Adds a change listener. It will be called any time an action is dispatched, and some part of the state tree may potentially have changed.It is a low-level API. Most likely, instead of using it directly, you'll use React (or other) bindings replaceReducer : Replaces the reducer currently used by the store to calculate the state. It is an advanced API. You might need this if your app implements code splitting, and you want to load some of the reducers dynamically. You might also need this if you implement a hot reloading mechanism for Redux.
  5. We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead. We return the previous state in the default case. It's important to return the previous state for any unknown action.
  6. We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead. We return the previous state in the default case. It's important to return the previous state for any unknown action.
  7. We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead. We return the previous state in the default case. It's important to return the previous state for any unknown action.
  8. We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead. We return the previous state in the default case. It's important to return the previous state for any unknown action.
  9. Flux is a pattern and Redux is a library. Flux is a fancy name for the observer pattern modified a little bit to fit React, but Facebook released a few tools to aid in implementing the Flux pattern, so the following is the difference between using these tools (which is commonly referred to as using Flux) and using Redux.
  10. Any middleware you use can then intercept anything you dispatch, and in turn, can pass actions to the next middleware in the chain. For example, a Promise middleware can intercept Promises and dispatch a pair of begin/end actions asynchronously in response to each Promise. When the last middleware in the chain dispatches an action, it has to be a plain object. This is when the synchronous Redux data flow takes place.
  11. Redux-Thunk is really easy to learn as there are literally no new concepts involved at all. Everything is basic javascript and therefore makes it easy to pick up and integrate into your application. On the flipside it does not scale that well and as the amount of your async logic grows you might end up with rather complicated code. Furthermore it might no longer be obvious, which action creators are pure and which have sideeffects attached to them, which further complicates the flow of your application logic (although naming conventions might help to lessen this issue). Also there is no built in way to handle advanced tasks like throttling, debouncing or cancellation.
  12. Redux-Saga is an immensely powerful tool that comes packed with a lot of useful helper functions. E.g. it already has built in ways to handle advanced tasks like throttling, debouncing, race conditions and cancellation. It is also very well documented. The downsides are that you really have to understand generator functions first. While these are a powerful tool in you belt, they are currently rarely used outside of the Redux-Saga world, which means that this particular skill might not be as transferable to other fields as lets say Redux-Observable, which is built on reactive streams. Redux-Saga code also tends to be rather imperative. Depending on your personal preferences this might be either a pro or a con. Another painpoint with sagas is testing. It is certainly doable, but can be quite confusing at times. Because sagas live outside of your other redux logic it also might not always be obvious, where sideeffects actually happen.
  13. Redux-Observable basically shares most of the pros and cons of Redux-Saga. It ist very powerful, has cancellation, throttling etc. already baked in, but comes with a steep learning curve and somewhat esoteric testing via a testScheduler. Another downside to Redux-Observable is, that your async logic again is separated from your other redux related code.
  14. Because our async handling resides directly next to our other redux logic it is really easy to follow the flow of actions. Commands just declaratively describe effects and so our reducers as well as our action creators remain pure functions. This makes our code easily testable (Redux-Loop comes with a few helper functions for testing commands as well). Another big pro is that there are just a few new concepts to learn. I assume that most developers will be able to pick up Redux-Loop almost as quickly as they would pick up Redux-Thunk. One major downside compared to Redux-Observable and Redux-Saga is, that Redux-Loop does not come with any additional helpers for cancellation, throttling or debouncing. So by default it is not quite as powerful as its major competitors.