SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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 잘 (하는 척) 하기
 

Similar a Redux Saga - Under the hood

Similar a 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
 

Más de Waqqas Jabbar

Más 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
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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 Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
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
 

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