SlideShare a Scribd company logo
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: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedVisual Engineering
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareVisual 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: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
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

Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 

Recently uploaded (20)

Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 

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.