SlideShare a Scribd company logo
1 of 13
REDUX JS
Content
● Architecture of Redux
● Actions
● Reducers
● Store
● Containers and Components
Redux
Architecture revolves around a strict unidirectional dataflow
Actions
● An action is a plain object that represents an intention to change the state.
● Actions are the only way to get data and update into the store.
● Simply that represent the fact about “what happened”.
● Example
export Const USER_LOGIN = 'USER_LOGIN';
export var userlogin=(data) =>
({
type: USER_LOGIN,
payLoad: data
})
● Actions must have a type field that indicates the type of action being performed.
● Where as Payload is optional.If u want to upload anything then only u have to pass payload
otherwise not required.
● Refer Here :- http://redux.js.org/docs/basics/Actions.html
Reducers
● A reducer (also called a reducing function) is a function that accepts an accumulation and a value
and returns a new accumulation.
● Simply the reducer is a pure function that takes the previous state and an action, and returns the
next state.
type Reducer<S, A> = (state: S, action: A) => S
● Reducers are used to update the state according to those actions triggered.
● Designing the state shape is very important.
● Basic syntax:
● (previous State,action) => newState
export const Reducer_Name(previous_state/initial State ,action) => {
switch(action.type)
case (action) {
return() //returns new state finally
}
}
● 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.
● Note :- Do not put API calls into reducers
● Refer Here :- http://redux.js.org/docs/basics/Reducers.html
Combine Reducers
● Combine reducer are used to combine to combine several reducers into one.
● Basic syntax:
Const App_Reducer = combineReducer ({
reducer_1: Reducer_1,
reducer_2:Reducer_2,
reducer_3:Reducer_3
});
export default App_Reducer ;
● Now this App_Reducer can is used to createStore for your Application using
createStore(reducer)
Syntax :- let store = createStore(App_reducer).
Store
● Reducers + Actions.
● A store is an single object that holds the entire application's state tree.
● There should only be a single store in your app.
● Methods:-
Allows state to be updated via dispatch (action)
Allows access to state via getState()
Registers listeners via subscribe(listener)
● Every time the state changes,subscribe logs automatically Calls the reducer.
● Holds the application state.
● Src :- http://redux.js.org/docs/basics/Store.html
Integrating with react:
Passing the Store: All container components need access to the Redux store
1.One option would be to pass it as a prop to every container component
2.use a special React Redux component called <Provider> to make the store available to all container components in the
application without passing it explicitly.
Basic syntax:
import todoApp from './reducers'
Let store =createStore(appReducer);
return (
<provider store={{store}} ><App/></provider>
document.getElementById('root')
);
Refer Here : https://www.npmjs.com/package/redux-logger
Methods to access them in component:
[mapStateToProps(state, [ownProps]): stateProps] (Function):
● That tells how to transform the current Redux store state into the props you want to pass to a
component you are wrapping.
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function):
● That receives the dispatch() method and returns callback props that you want to inject into the
component.
● Src :- https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options
Loggers for redux(Integration)
● npm i --save redux-logger install
import { applyMiddleware, createStore } from 'redux';
import { createLogger } from 'redux-logger'
let logger= createLogger({...options});
Pass this logger to applymiddleware as second argument for createStore
const store = createStore(reducer,applyMiddleware(logger));
Src :- https://www.npmjs.com/package/redux-logger
Persist and Rehydrate a redux store.
npm i --save redux-persist
import {compose, applyMiddleware, createStore} from 'redux'
import {persistStore, autoRehydrate} from 'redux-persist'
(note: `autoRehydrate` is not a middleware)
const store = createStore(
reducer,
compose(
applyMiddleware(...),
autoRehydrate()
)
)
persistStore(store)
.
Src :- https://www.npmjs.com/package/redux-persist
The autoRehydrate() enhancer
forces the app to “rehydrate”
(a.k.a. reload) the persisted app
state on startup.
Combining these two results in
showing the user the very same
screen where they left the app,
even if it was closed in the
meantime.
Final OverView of Redux
● Redux is a JavaScript library that aims to simplify how we manage stateful data.
● Redux keeps all of our data in a single JS object called the Store.
● A single function, the reducer, is responsible for making modifications to the Store.
● We trigger the reducer by 'dispatching' an action - a JS object that describes how our data should
change.
● The reducer function receives the action as an argument and makes changes accordingly.
● Other parts of the code (usually React Components) can subscribe to data in the Store.
● When data changes, Redux notifies subscribers of the change.
Reference
● http://redux.js.org (Official Doc for Redux).

More Related Content

What's hot

Redux training
Redux trainingRedux training
Redux trainingdasersoft
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practicesClickky
 
What's new in jQuery 1.5
What's new in jQuery 1.5What's new in jQuery 1.5
What's new in jQuery 1.5Martin Kleppe
 
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max PetruckMaksym Petruk
 
Richard Fridrich: Třesení stromem v JavaScriptu
Richard Fridrich: Třesení stromem v JavaScriptuRichard Fridrich: Třesení stromem v JavaScriptu
Richard Fridrich: Třesení stromem v JavaScriptuDevelcz
 
Discussion of NGRX-Entity
Discussion of NGRX-EntityDiscussion of NGRX-Entity
Discussion of NGRX-EntityNate Kidwell
 
Architecting JavaScript Code
Architecting JavaScript CodeArchitecting JavaScript Code
Architecting JavaScript CodeSuresh Balla
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and reduxCuong Ho
 
Sprint 39 review
Sprint 39 reviewSprint 39 review
Sprint 39 reviewManageIQ
 
An Introduction To PostgreSQL Triggers
An Introduction To PostgreSQL TriggersAn Introduction To PostgreSQL Triggers
An Introduction To PostgreSQL TriggersJim Mlodgenski
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples Ravi Mone
 
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012Amazon Web Services
 
GR8Conf 2011: Grails Webflow
GR8Conf 2011: Grails WebflowGR8Conf 2011: Grails Webflow
GR8Conf 2011: Grails WebflowGR8Conf
 
Streaming data to s3 using akka streams
Streaming data to s3 using akka streamsStreaming data to s3 using akka streams
Streaming data to s3 using akka streamsMikhail Girkin
 
Grails transactions
Grails   transactionsGrails   transactions
Grails transactionsHusain Dalal
 

What's hot (18)

Redux training
Redux trainingRedux training
Redux training
 
React with Redux
React with ReduxReact with Redux
React with Redux
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
What's new in jQuery 1.5
What's new in jQuery 1.5What's new in jQuery 1.5
What's new in jQuery 1.5
 
React и redux
React и reduxReact и redux
React и redux
 
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max Petruck
 
Richard Fridrich: Třesení stromem v JavaScriptu
Richard Fridrich: Třesení stromem v JavaScriptuRichard Fridrich: Třesení stromem v JavaScriptu
Richard Fridrich: Třesení stromem v JavaScriptu
 
Discussion of NGRX-Entity
Discussion of NGRX-EntityDiscussion of NGRX-Entity
Discussion of NGRX-Entity
 
Architecting JavaScript Code
Architecting JavaScript CodeArchitecting JavaScript Code
Architecting JavaScript Code
 
React 101
React 101React 101
React 101
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
 
Sprint 39 review
Sprint 39 reviewSprint 39 review
Sprint 39 review
 
An Introduction To PostgreSQL Triggers
An Introduction To PostgreSQL TriggersAn Introduction To PostgreSQL Triggers
An Introduction To PostgreSQL Triggers
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples
 
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
MBL301 Data Persistence to Amazon Dynamodb for Mobile Apps - AWS re: Invent 2012
 
GR8Conf 2011: Grails Webflow
GR8Conf 2011: Grails WebflowGR8Conf 2011: Grails Webflow
GR8Conf 2011: Grails Webflow
 
Streaming data to s3 using akka streams
Streaming data to s3 using akka streamsStreaming data to s3 using akka streams
Streaming data to s3 using akka streams
 
Grails transactions
Grails   transactionsGrails   transactions
Grails transactions
 

Similar to Redux

Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux jsCitrix
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareVisual Engineering
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedVisual Engineering
 
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxSH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxMongoDB
 
Using React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIsUsing React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIsMihail Gaberov
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptxNavneetKumar111924
 
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
 
Getting started with React and Redux
Getting started with React and ReduxGetting started with React and Redux
Getting started with React and ReduxPaddy Lock
 
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass PluginJS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass PluginJSFestUA
 
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
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupDavid Barreto
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsEvangelia Mitsopoulou
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingBinary Studio
 

Similar to Redux (20)

Redux workshop
Redux workshopRedux workshop
Redux workshop
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
 
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptxSH 1 - SES 6 - compass-tel-aviv-slides.pptx
SH 1 - SES 6 - compass-tel-aviv-slides.pptx
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
React Redux AntD and Umi js
React Redux AntD and Umi jsReact Redux AntD and Umi js
React Redux AntD and Umi js
 
React js
React jsReact js
React js
 
Using React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIsUsing React, Redux and Saga with Lottoland APIs
Using React, Redux and Saga with Lottoland APIs
 
Redux
ReduxRedux
Redux
 
Let's start with REDUX
Let's start with REDUXLet's start with REDUX
Let's start with REDUX
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
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)
 
Getting started with React and Redux
Getting started with React and ReduxGetting started with React and Redux
Getting started with React and Redux
 
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass PluginJS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
JS Fest 2018. Anna Herlihy. How to Write a Compass Plugin
 
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
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance Meetup
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applications
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 

Redux

  • 2. Content ● Architecture of Redux ● Actions ● Reducers ● Store ● Containers and Components
  • 3. Redux Architecture revolves around a strict unidirectional dataflow
  • 4. Actions ● An action is a plain object that represents an intention to change the state. ● Actions are the only way to get data and update into the store. ● Simply that represent the fact about “what happened”. ● Example export Const USER_LOGIN = 'USER_LOGIN'; export var userlogin=(data) => ({ type: USER_LOGIN, payLoad: data }) ● Actions must have a type field that indicates the type of action being performed. ● Where as Payload is optional.If u want to upload anything then only u have to pass payload otherwise not required. ● Refer Here :- http://redux.js.org/docs/basics/Actions.html
  • 5. Reducers ● A reducer (also called a reducing function) is a function that accepts an accumulation and a value and returns a new accumulation. ● Simply the reducer is a pure function that takes the previous state and an action, and returns the next state. type Reducer<S, A> = (state: S, action: A) => S ● Reducers are used to update the state according to those actions triggered. ● Designing the state shape is very important. ● Basic syntax: ● (previous State,action) => newState export const Reducer_Name(previous_state/initial State ,action) => { switch(action.type) case (action) { return() //returns new state finally } } ● 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. ● Note :- Do not put API calls into reducers ● Refer Here :- http://redux.js.org/docs/basics/Reducers.html
  • 6. Combine Reducers ● Combine reducer are used to combine to combine several reducers into one. ● Basic syntax: Const App_Reducer = combineReducer ({ reducer_1: Reducer_1, reducer_2:Reducer_2, reducer_3:Reducer_3 }); export default App_Reducer ; ● Now this App_Reducer can is used to createStore for your Application using createStore(reducer) Syntax :- let store = createStore(App_reducer).
  • 7. Store ● Reducers + Actions. ● A store is an single object that holds the entire application's state tree. ● There should only be a single store in your app. ● Methods:- Allows state to be updated via dispatch (action) Allows access to state via getState() Registers listeners via subscribe(listener) ● Every time the state changes,subscribe logs automatically Calls the reducer. ● Holds the application state. ● Src :- http://redux.js.org/docs/basics/Store.html
  • 8. Integrating with react: Passing the Store: All container components need access to the Redux store 1.One option would be to pass it as a prop to every container component 2.use a special React Redux component called <Provider> to make the store available to all container components in the application without passing it explicitly. Basic syntax: import todoApp from './reducers' Let store =createStore(appReducer); return ( <provider store={{store}} ><App/></provider> document.getElementById('root') ); Refer Here : https://www.npmjs.com/package/redux-logger
  • 9. Methods to access them in component: [mapStateToProps(state, [ownProps]): stateProps] (Function): ● That tells how to transform the current Redux store state into the props you want to pass to a component you are wrapping. [mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): ● That receives the dispatch() method and returns callback props that you want to inject into the component. ● Src :- https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options
  • 10. Loggers for redux(Integration) ● npm i --save redux-logger install import { applyMiddleware, createStore } from 'redux'; import { createLogger } from 'redux-logger' let logger= createLogger({...options}); Pass this logger to applymiddleware as second argument for createStore const store = createStore(reducer,applyMiddleware(logger)); Src :- https://www.npmjs.com/package/redux-logger
  • 11. Persist and Rehydrate a redux store. npm i --save redux-persist import {compose, applyMiddleware, createStore} from 'redux' import {persistStore, autoRehydrate} from 'redux-persist' (note: `autoRehydrate` is not a middleware) const store = createStore( reducer, compose( applyMiddleware(...), autoRehydrate() ) ) persistStore(store) . Src :- https://www.npmjs.com/package/redux-persist The autoRehydrate() enhancer forces the app to “rehydrate” (a.k.a. reload) the persisted app state on startup. Combining these two results in showing the user the very same screen where they left the app, even if it was closed in the meantime.
  • 12. Final OverView of Redux ● Redux is a JavaScript library that aims to simplify how we manage stateful data. ● Redux keeps all of our data in a single JS object called the Store. ● A single function, the reducer, is responsible for making modifications to the Store. ● We trigger the reducer by 'dispatching' an action - a JS object that describes how our data should change. ● The reducer function receives the action as an argument and makes changes accordingly. ● Other parts of the code (usually React Components) can subscribe to data in the Store. ● When data changes, Redux notifies subscribers of the change.