SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Redux Saga
Waqqas Jabbar
Pre-requisites
● Basic JS concepts: Promise
● Basic concepts of React.js
● Preferred: Basic concepts of Redux
React.js
“A JavaScript library for building user interfaces”
● You need other components to make a fully functional app
○ Redux for state management
○ API calls
Redux
Basic Redux Life-cycle
Middleware
Put code between
dispatching an action
and
reaching a reducer
Redux Life-cycle with middlewares
Redux-Saga: Advantages
● Write Async code a synchronous
○ More readable
● I.e. Write business logic in a single location
● Easy Testing
● Written as generator functions
Generator Functions (1/3)
● Generator functions are functions that can start, paused,
and resumed. It maintains its state
● yield defines one iteration
● First iteration: {“value”: “one”, done: false}
Generator functions (2/3)
● {“value”: 0, done: false}
● {“value”: 1, done: false}
● {“value”: 21, done: false}
● {“value”: 321, done: false}
Generator functions (3/3)
Commonly used helper functions
● take(takeLatest, takeEvery): handle an action
● call: call a function that return promise
● put: generate a new action
● select: get data from redux
●
● fork: fork a new “thread”
● channel
Example: API Call
● Generate action: GET_LIST_REQUEST
● Saga
○ takeLatest(GET_LIST_REQUEST, getListRequest)
● getListRequest(api, action){
○ const response = yield call(api.getList)
○ if(response.ok)
■ yield put(getListRequestSuccess(response.data))
○ else
■ yield put(getListRequestFailure(response))
IMPORTANT: Some actions for sagas, other for redux
EXAMPLE: HANDLING CALLBACKS IN SAGAS
● Limitation: Cannot call yield in callback function in
generator function
● Solution: use “channel”
● const passThroughChannel = channel();
● takeEvery(passThroughChannel, passThroughChannelWatcher)
● const passThroughChannelWatcher = function* (action) {
● yield put(action);
● };
● Alert.alert()
○ onPress: passThroughChannel.put(actionCreator())
Thank You
Coming Soon
React-Native Training
Twitter: @waqqasjabbar
Medium:
https://medium.com/@waqqas

Contenu connexe

Tendances

Tendances (20)

Getting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) AngularGetting Started with NgRx (Redux) Angular
Getting Started with NgRx (Redux) Angular
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Redux Thunk
Redux ThunkRedux Thunk
Redux Thunk
 
React hooks
React hooksReact hooks
React hooks
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Avoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promisesAvoiding callback hell in Node js using promises
Avoiding callback hell in Node js using promises
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
 
react redux.pdf
react redux.pdfreact redux.pdf
react redux.pdf
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React Router: React Meetup XXL
React Router: React Meetup XXLReact Router: React Meetup XXL
React Router: React Meetup XXL
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
React lecture
React lectureReact lecture
React lecture
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Ngrx: Redux in angular
Ngrx: Redux in angularNgrx: Redux in angular
Ngrx: Redux in angular
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Ngrx
NgrxNgrx
Ngrx
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기
 

Similaire à Redux Saga - Under the hood

Similaire à Redux Saga - Under the hood (20)

Jump into React-Native (Class 5)
Jump into React-Native  (Class 5)Jump into React-Native  (Class 5)
Jump into React-Native (Class 5)
 
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
 
Aplicaciones en tiempo real con ReactJS y SignalR Core
Aplicaciones en tiempo real con ReactJS y SignalR CoreAplicaciones en tiempo real con ReactJS y SignalR Core
Aplicaciones en tiempo real con ReactJS y SignalR Core
 
Why we built 35kb React Native alternative ?
Why we built 35kb React Native alternative ?Why we built 35kb React Native alternative ?
Why we built 35kb React Native alternative ?
 
How To Upgrade The React 18 Release Candidate.pptx
How To Upgrade The React 18 Release Candidate.pptxHow To Upgrade The React 18 Release Candidate.pptx
How To Upgrade The React 18 Release Candidate.pptx
 
Entender React Native
Entender React NativeEntender React Native
Entender React Native
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
 
Going Reactive with Spring 5
Going Reactive with Spring 5Going Reactive with Spring 5
Going Reactive with Spring 5
 
Single Page Applications with AngularJS 2.0
Single Page Applications with AngularJS 2.0 Single Page Applications with AngularJS 2.0
Single Page Applications with AngularJS 2.0
 
Effects, Coeffects & Subscriptions: a pit of success for SPAs
Effects, Coeffects & Subscriptions: a pit of success for SPAsEffects, Coeffects & Subscriptions: a pit of success for SPAs
Effects, Coeffects & Subscriptions: a pit of success for SPAs
 
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
 
Play Framework
Play FrameworkPlay Framework
Play Framework
 
Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideDeveloping Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
 
React Redux AntD and Umi js
React Redux AntD and Umi jsReact Redux AntD and Umi js
React Redux AntD and Umi js
 
On component interface
On component interfaceOn component interface
On component interface
 
Introduction to Redux.pptx
Introduction to Redux.pptxIntroduction to Redux.pptx
Introduction to Redux.pptx
 
An overview of node.js
An overview of node.jsAn overview of node.js
An overview of node.js
 
Reactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xReactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.x
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 

Plus de Waqqas Jabbar

Plus de Waqqas Jabbar (13)

Introduction to react native @ TIC NUST
Introduction to react native @ TIC NUSTIntroduction to react native @ TIC NUST
Introduction to react native @ TIC NUST
 
Jump into React-Native (Class 6)
Jump into React-Native  (Class 6)Jump into React-Native  (Class 6)
Jump into React-Native (Class 6)
 
Jump into React-Native (Class 4)
Jump into React-Native  (Class 4)Jump into React-Native  (Class 4)
Jump into React-Native (Class 4)
 
Jump into React Native (Class 3)
Jump into React Native  (Class 3)Jump into React Native  (Class 3)
Jump into React Native (Class 3)
 
Jump into React-Native (Class 2/6)
Jump into React-Native  (Class 2/6)Jump into React-Native  (Class 2/6)
Jump into React-Native (Class 2/6)
 
Jump into React-Native (Class 1)
Jump into React-Native (Class 1)Jump into React-Native (Class 1)
Jump into React-Native (Class 1)
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
Crud application using react native, redux and redux sagas
Crud application using react native, redux and redux sagasCrud application using react native, redux and redux sagas
Crud application using react native, redux and redux sagas
 
Introduction To SVN
Introduction To SVNIntroduction To SVN
Introduction To SVN
 
Makefile Generation From Autotools
Makefile Generation From AutotoolsMakefile Generation From Autotools
Makefile Generation From Autotools
 
Introduction To Makefile
Introduction To MakefileIntroduction To Makefile
Introduction To Makefile
 
Introduction To SVN
Introduction To SVNIntroduction To SVN
Introduction To SVN
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Redux Saga - Under the hood

  • 2. Pre-requisites ● Basic JS concepts: Promise ● Basic concepts of React.js ● Preferred: Basic concepts of Redux
  • 3. React.js “A JavaScript library for building user interfaces” ● You need other components to make a fully functional app ○ Redux for state management ○ API calls
  • 6. Middleware Put code between dispatching an action and reaching a reducer
  • 7. Redux Life-cycle with middlewares
  • 8. Redux-Saga: Advantages ● Write Async code a synchronous ○ More readable ● I.e. Write business logic in a single location ● Easy Testing ● Written as generator functions
  • 9. Generator Functions (1/3) ● Generator functions are functions that can start, paused, and resumed. It maintains its state ● yield defines one iteration ● First iteration: {“value”: “one”, done: false}
  • 10. Generator functions (2/3) ● {“value”: 0, done: false} ● {“value”: 1, done: false} ● {“value”: 21, done: false} ● {“value”: 321, done: false}
  • 12. Commonly used helper functions ● take(takeLatest, takeEvery): handle an action ● call: call a function that return promise ● put: generate a new action ● select: get data from redux ● ● fork: fork a new “thread” ● channel
  • 13. Example: API Call ● Generate action: GET_LIST_REQUEST ● Saga ○ takeLatest(GET_LIST_REQUEST, getListRequest) ● getListRequest(api, action){ ○ const response = yield call(api.getList) ○ if(response.ok) ■ yield put(getListRequestSuccess(response.data)) ○ else ■ yield put(getListRequestFailure(response)) IMPORTANT: Some actions for sagas, other for redux
  • 14. EXAMPLE: HANDLING CALLBACKS IN SAGAS ● Limitation: Cannot call yield in callback function in generator function ● Solution: use “channel” ● const passThroughChannel = channel(); ● takeEvery(passThroughChannel, passThroughChannelWatcher) ● const passThroughChannelWatcher = function* (action) { ● yield put(action); ● }; ● Alert.alert() ○ onPress: passThroughChannel.put(actionCreator())
  • 15. Thank You Coming Soon React-Native Training Twitter: @waqqasjabbar Medium: https://medium.com/@waqqas