SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
Redux and
Context API with
React Native
App:
Introduction,
Use Cases,
Implementation,
and Comparison
www.bacancytechnology.com
Developer 1 and Developer 2 were fighting
over which state management tool is
better- Redux or Context API. They both
listed pros and cons but sadly none realized
the actual use case and purpose of Redux
and Context API.


If you’re one of the developer 1 or 2, then
continue reading the tutorial to learn why it
shouldn’t be Redux vs Context API.
Introduction
If you’re having a Javascript background
then you might be familiar with the terms
Redux and Context API. And probably, you
might have come across so many blogs that
debate on which is better- Redux vs Context
API. I assumed the same unless I realized
it’s not!


After reading a bunch of blogs, I concluded
the purpose of both the tools and how
different they are from each other. If you
aren’t aware yet, don’t worry this tutorial
will help you understand the use cases of
both tools with a basic demo example. Here,
we will build an app using both approaches
and discuss them.


Let’s get started, then!
Tutorial Goal
Understanding Redux and Context API
Comparing the working of Redux and
Context API
Exploring the purpose and use case of
Redux and Context API
A demo application using Redux and
Context API approach
Redux:
Introduction
and Building
Blocks
According to documentation-


Redux is a pattern and library for managing
and updating application state, using events
called “actions”. It serves as a centralized
store for state that needs to be used across
your entire application, with rules ensuring
that the state can only be updated in a
predictable fashion.


The documentation clearly mentions that
redux is for “managing state” and
understanding how the state is updated.
The primary goal of redux, as
mentioned in the doc, is to manage and
keep track of the state.
Keeping your state management logic
separate from the user interface layer
Faster logic debugging


Redux is mainly used to manage the state of
React applications in a centralized place
where to access the state anywhere in the
application. Technically, The concept of
Redux is based on Flux architecture and this
concept isn’t restricted to React apps; there
are implementations in different
technologies, as well (e.g. NgRx for Angular).
But Redux is particularly implemented with
React.


Use Cases of Redux
redux: For the functions like
createStore(), combineReducer() etc.
react-redux: For the functions like
connect() etc.
Packages needed
Building Blocks of Redux
It consists of mainly four building blocks:


1. Reducer: These are functions with state
and actions passed in as arguments. It
contains “action.type” in switch cases which
returns the changed value. It optionally
accepts the payload (generally created in a
separate file known as reducers.js)


2. Store: Store is the collection of all data.
You can pass it to the provider.
3. Provider: A React component that accepts
store as an argument (usually created in
index.js)


4. Actions: Functions that provide/return
action type and payload to the dispatcher
which will further call the respective
reducer (generally created in a separate file
known as actions.js)
Context API:
Introduction
and Building
Blocks
React documentation explains context API
as-


Context provides a way to pass data
through the component tree without having
to pass props down manually at every level.


In a typical React application, data is passed
top-down (parent to child) via props, but
such usage can be cumbersome for certain
types of props (e.g. locale preference, UI
theme) that are required by many
components within an application. Context
provides a way to share values like these
between components without having to
explicitly pass a prop through every level of
the tree.


If you can observe the documentation
states context for “passing” and “sharing”
values and mention nothing about
“managing the state”
The main purpose of using context API is
avoiding ‘prop drilling’ – passing prop at every
level. It is more like a pipeline used to pass
values from one end to another.


Context API provides the easiest way for
passing data through the component tree so
that you don’t have to pass props down
manually at every level. For example, assume
we have a component tree consisting of A, B, C,
and D components. Now you need to pass props
from A to D, so rather than passing it A > B > C >
D,i.e., to every component, with the help of
context you can directly pass to A > D.


Now, many blogs have mentioned that Context
API is the best replacement for Redux because
it’s in-built and you don’t have to install
dependencies for the same. Is this really true-
Can Context API replace Redux? We will discuss
this in a further section. Stay tuned to explore!


Use Cases of Context API
We can divide the Context API into three
blocks:


1. Context: Use createContext() function
that takes the default value as a first
argument. Here it is optional to pass a
Javascript object. You can implement
multiple contexts in your app.


2. Provider: After creating context, the
Provider provides the capability for
accessing the context. It provides
functions & data to pass values further to
the component.


3. Consumer: Consumer allows access to
the value to child components which are
wrapped by Provider. It has two types-




Building Blocks of Context
API
Context.Consumer: Context.Consumer
can be used for both functional and
class-based components. However,
through this approach, the context is
accessible within the render method
only.
Static ContextType: Static
contextType can be used only for
Class-based components.
Redux and
Context API
Example
The example below is based on a
Counter. The initial value will be 0 and
it has two buttons to increment and
decrement the value.
Inside the main parent counter
component, there will be three child
components-
one for changing the counter value
two for each of the buttons.


The initial setup would be the same for both
Context and Redux approaches.
Create a React Native App
Initially, create a react native app using the
following command
react-native init CounterDemo
Redux
Approach:
How to
Implement
Redux in
React Native
App?
Install Required dependencies for Redux


npm install redux --save
npm install react-redux --save


Redux Store Set Up
We will create our store within our App.js
file.
// App.js
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import totalReducers from
'./reducers/index.js';
import Counter from
'./components/counters';
const store = createStore(totalReducers);
export default class App extends
Component{
render(){
return(
);
}
}
Here we are importing totalReducers
from the reducers folder.
createStore() function accepts one
argument as totalReducers object and
generates the store.
The Provider component makes sure
the store is available throughout the
application.
Reducers Set Up


Reducers return the data required by the
application. In this demo, the reducer will
return the updated counter value. Here is
our counterReducer.js file inside the
reducers folder.
// reducers/counterReducer.js
let count= 0;
export default (state = count , action) => {
switch (action.type) {
case "Increment":
count ++
break;
case "Decrement":
count --
break;
default:
count;
}
return count;
}
The reducer defined above will always
return the count value.
Increment & Decrement are the actions
types that will update the value as shown
above.
We will combine all the reducers inside
the index.js file inside the reducers
folder.
Explanation
// reducers/index.js
import {combineReducers} from 'redux';
import counterReducer from
'./counterReducer';
const totalReducers= combineReducers({
count: counterReducer,
});
export default totalReducers;
Create two actions: Increment & Decrement.


// actions/index.js


export function increment(){
return{
type: "Increment"
};
}
export function decrement(){
return{
type: "Decrement"
};
}
Here we will combine all the reducers as
arguments of the combineReducers()
function of the Redux library.
Explanation


Actions Set Up
mapStateToProps() – It simply accepts
your reducer data, and converts it into a
simple usable prop. With the help of
this.props.data we will use data as a
prop in our component.
We will simply create only one component
called a counter component. For using
reducers and actions, we have to implement
these functions:


function mapStateToProps(state){
return{
count: state.count
};
}
UI Component
mapDispatchToProps() – It accepts your
actions, and converts them into a simple
usable prop.
Note: Keep in mind how we assigned names
to reducers in the combineReducers
function because we have to use the same
name for calling respective reducers.


function mapDispatchToProps(dispatch){
return bindActionCreators({increment,
decrement}, dispatch)
}
Note: bindActionCreators function simply
combines our actions into one object.


Moving towards the component that
manages the user interface.


// component/counter.js
class Counters extends Component {
constructor(props) {
super(props);
}
render() {
return (
{'The Redux Approach'}
{this.props.count}
this.props.increment()}>
Increment + this.props.decrement()}>
Decrement -
);
}
}
function mapStateToProps(state) {
return {
count: state.count,
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({increment,
decrement}, dispatch);
}
export default connect(mapStateToProps,
mapDispatchToProps)(Counters);
For styling your component you can use the
below code
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
counterNumber: {
fontSize: 35,
fontWeight: 'bold',
},
buttonContainer: {
flexDirection: 'row',
},
buttonStyle: {
backgroundColor: 'green',
borderWidth: 1,
height: 30,
width: '25%',
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
},
});
Finishing up


So far we are done with redux set up, logic,
and user interface for the counter demo.
Now, just a simple step remaining – import
our App file in our index.js file.


// index.js
import {AppRegistry} from 'react-native';
import App from './src/App.js';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName,
() => App);
Git Repo link: https://github.com/sunil-
bacancy/CounterDemo
Context API
Approach:
How to
Implement
Context API
in React
Native App?
Since the context API is in-built
functionality we don’t need to install third-
party dependencies.
Folder structure
Create a folder src at the root of your app.
Within the src folder, we have to create 3
folders that are reducers, components, state
and one file App.js.
Just like Redux, declare reducer with
Context API.


// reducers/globalReducer.js
export default countReducer = (state , action) => {
switch (action.type) {
case "Increment":
return{
...state,
counter: state.counter + 1,
}
case "Decrement":
return{
...state,
counter: state.counter - 1,
}
default:
return{
state
}
}
}


Reducer Set Up
Create the context
Create a context using createContext()
and pass the initial state as arguments.
You can also define without passing
arguments.
Define a function that will pass the data
via Provider.
useReducer() will take a reducer having
default state, then return the updated
value and dispatch the function.
Inside the Provider function, use
useReducer() with arguments- reducer
and initial state. The returned and
dispatched states are later passed as
values to the Provider.
// state/globalState.js
import React, {createContext, useReducer}
from 'react';
import countReducer from
'../reducers/globalReducer';
const initialState = {
counter: 0
}
export const GlobalContext =
createContext(initialState);
export default GlobalProvider = ({children}) => {
const [state, dispatch] =
useReducer(countReducer, initialState);
return(
{children}
)
}
Providing Context


After creating the context, we need to
provide the context so that it is accessible in
the child components. For that, you need to
wrap it within the Provider.


// src/App.js
import React, { Component } from 'react';
import GlobalProvider from
'./state/globalState';
import Counters from
'./components/counter';
export default class App extends Component
{
render(){
return (
)
}
}
Consuming Context


Use useContext() for consuming the context
in respective child components.


// components/counter.js
import React, { Component, useContext }
from 'react';
import { View, Text, TouchableOpacity,
StyleSheet } from 'react-native';
import {GlobalContext} from
'../state/globalState';
const Counters = () => {
const {state} = useContext(GlobalContext);
const {dispatch} =
useContext(GlobalContext);
return(
{'The Context API Approach' }
{ state.counter }
dispatch({type: "Increment"})}
>
Increment +
dispatch({type: "Decrement"})}
>
Decrement -
)
}
export default Counters;
For styling your component you can use the
below code.
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
counterNumber: {
fontSize: 35,
fontWeight: 'bold'
},
buttonContainer: {
flexDirection: 'row'
},
buttonStyle: {
backgroundColor: 'green',
borderWidth: 1,
height: 30,
width: '25%',
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
},
})
Git Repo link: https://github.com/sunil-
bacancy/CounterContextDemo
Redux and
Context API:
Comparison
Redux
Storing and managing values
Works outside React components
Avoids prop-drilling
Through dispatching an action one can update
the value
Provides DevTools to show history of actions
and state values
Allows application code for triggering side
effects through middlewares
Context API
Not for storing or managing values
Works in React components only
Passes a single value be it objects, primitives,
classes, etc.
Avoids prop-drilling
Provides current context value for Provider
and Consumer but doesn’t display any history
of how the value is changed.
Excludes side effects mechanism – exclusively
for component rendering
Conclusion
I hope the tutorial helped you understand
how different Context and Redux are.
Moreover, try to implement both the
approaches and play around with the code
to dig deeper. For more such React Native
tutorials, we have a React Native Tutorials
page that consists of step-by-step
guidelines with the github source.


I can understand how difficult it can be to
organize and manage a global store for a
complex application, and also how precise
you need to be for implementing Context
API in your application. In case you have a
large and complex project and want to
implement Redux or Context API then feel
free to contact us and hire React Native
developer.
Thank You
www.bacancytechnology.com

Contenu connexe

Tendances

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 hookPiyush Jamwal
 
Important React Hooks
Important React HooksImportant React Hooks
Important React HooksKnoldus Inc.
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core ConceptsDivyang Bhambhani
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overviewAlex Bachuk
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux jsCitrix
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSKnoldus Inc.
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsANKUSH CHAVAN
 
Domain driven design 8장
Domain driven design 8장Domain driven design 8장
Domain driven design 8장kukuman
 
State management in react applications (Statecharts)
State management in react applications (Statecharts)State management in react applications (Statecharts)
State management in react applications (Statecharts)Tomáš Drenčák
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 

Tendances (20)

React hooks
React hooksReact hooks
React hooks
 
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
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
React js
React jsReact js
React js
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
React workshop
React workshopReact workshop
React workshop
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
Domain driven design 8장
Domain driven design 8장Domain driven design 8장
Domain driven design 8장
 
Cucumber & gherkin language
Cucumber & gherkin languageCucumber & gherkin language
Cucumber & gherkin language
 
State management in react applications (Statecharts)
State management in react applications (Statecharts)State management in react applications (Statecharts)
State management in react applications (Statecharts)
 
Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Angular vs react
Angular vs reactAngular vs react
Angular vs react
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 

Similaire à Redux vs Context API: Understanding use cases and building a demo app

React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)Chiew Carol
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with ReactNetcetera
 
what is context API and How it works in React.pptx
what is context API and How it works in React.pptxwhat is context API and How it works in React.pptx
what is context API and How it works in React.pptxBOSC Tech Labs
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfoliomwillmer
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 
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 malikLama K Banna
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react jsStephieJohn
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥Remo Jansen
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptxNavneetKumar111924
 
How To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxHow To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxBOSC Tech Labs
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdfArthyR3
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksKaty Slemon
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxBOSC Tech Labs
 

Similaire à Redux vs Context API: Understanding use cases and building a demo app (20)

React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with React
 
what is context API and How it works in React.pptx
what is context API and How it works in React.pptxwhat is context API and How it works in React.pptx
what is context API and How it works in React.pptx
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to 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
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Copy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdfCopy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdf
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
How To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxHow To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptx
 
Simple React Todo List
Simple React Todo ListSimple React Todo List
Simple React Todo List
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooks
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React context
React context  React context
React context
 

Plus de Katy Slemon

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfKaty Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfKaty Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfKaty Slemon
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfKaty Slemon
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfKaty Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfKaty Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfKaty Slemon
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdfHow to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdfKaty Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfKaty Slemon
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfKaty Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfKaty Slemon
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfKaty Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfKaty Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfKaty Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfKaty Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfKaty Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfKaty Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfKaty Slemon
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfKaty Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfKaty Slemon
 

Plus de Katy Slemon (20)

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdfHow to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdf
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
 

Dernier

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Dernier (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

Redux vs Context API: Understanding use cases and building a demo app

  • 1. Redux and Context API with React Native App: Introduction, Use Cases, Implementation, and Comparison www.bacancytechnology.com
  • 2. Developer 1 and Developer 2 were fighting over which state management tool is better- Redux or Context API. They both listed pros and cons but sadly none realized the actual use case and purpose of Redux and Context API. If you’re one of the developer 1 or 2, then continue reading the tutorial to learn why it shouldn’t be Redux vs Context API.
  • 4. If you’re having a Javascript background then you might be familiar with the terms Redux and Context API. And probably, you might have come across so many blogs that debate on which is better- Redux vs Context API. I assumed the same unless I realized it’s not! After reading a bunch of blogs, I concluded the purpose of both the tools and how different they are from each other. If you aren’t aware yet, don’t worry this tutorial will help you understand the use cases of both tools with a basic demo example. Here, we will build an app using both approaches and discuss them. Let’s get started, then!
  • 6. Understanding Redux and Context API Comparing the working of Redux and Context API Exploring the purpose and use case of Redux and Context API A demo application using Redux and Context API approach
  • 8. According to documentation- Redux is a pattern and library for managing and updating application state, using events called “actions”. It serves as a centralized store for state that needs to be used across your entire application, with rules ensuring that the state can only be updated in a predictable fashion. The documentation clearly mentions that redux is for “managing state” and understanding how the state is updated.
  • 9. The primary goal of redux, as mentioned in the doc, is to manage and keep track of the state. Keeping your state management logic separate from the user interface layer Faster logic debugging Redux is mainly used to manage the state of React applications in a centralized place where to access the state anywhere in the application. Technically, The concept of Redux is based on Flux architecture and this concept isn’t restricted to React apps; there are implementations in different technologies, as well (e.g. NgRx for Angular). But Redux is particularly implemented with React. Use Cases of Redux
  • 10. redux: For the functions like createStore(), combineReducer() etc. react-redux: For the functions like connect() etc. Packages needed Building Blocks of Redux It consists of mainly four building blocks: 1. Reducer: These are functions with state and actions passed in as arguments. It contains “action.type” in switch cases which returns the changed value. It optionally accepts the payload (generally created in a separate file known as reducers.js) 2. Store: Store is the collection of all data. You can pass it to the provider.
  • 11. 3. Provider: A React component that accepts store as an argument (usually created in index.js) 4. Actions: Functions that provide/return action type and payload to the dispatcher which will further call the respective reducer (generally created in a separate file known as actions.js)
  • 13. React documentation explains context API as- Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a typical React application, data is passed top-down (parent to child) via props, but such usage can be cumbersome for certain types of props (e.g. locale preference, UI theme) that are required by many components within an application. Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree. If you can observe the documentation states context for “passing” and “sharing” values and mention nothing about “managing the state”
  • 14. The main purpose of using context API is avoiding ‘prop drilling’ – passing prop at every level. It is more like a pipeline used to pass values from one end to another. Context API provides the easiest way for passing data through the component tree so that you don’t have to pass props down manually at every level. For example, assume we have a component tree consisting of A, B, C, and D components. Now you need to pass props from A to D, so rather than passing it A > B > C > D,i.e., to every component, with the help of context you can directly pass to A > D. Now, many blogs have mentioned that Context API is the best replacement for Redux because it’s in-built and you don’t have to install dependencies for the same. Is this really true- Can Context API replace Redux? We will discuss this in a further section. Stay tuned to explore! Use Cases of Context API
  • 15. We can divide the Context API into three blocks: 1. Context: Use createContext() function that takes the default value as a first argument. Here it is optional to pass a Javascript object. You can implement multiple contexts in your app. 2. Provider: After creating context, the Provider provides the capability for accessing the context. It provides functions & data to pass values further to the component. 3. Consumer: Consumer allows access to the value to child components which are wrapped by Provider. It has two types- Building Blocks of Context API
  • 16. Context.Consumer: Context.Consumer can be used for both functional and class-based components. However, through this approach, the context is accessible within the render method only. Static ContextType: Static contextType can be used only for Class-based components.
  • 18. The example below is based on a Counter. The initial value will be 0 and it has two buttons to increment and decrement the value. Inside the main parent counter component, there will be three child components- one for changing the counter value two for each of the buttons. The initial setup would be the same for both Context and Redux approaches. Create a React Native App Initially, create a react native app using the following command react-native init CounterDemo
  • 20. Install Required dependencies for Redux npm install redux --save npm install react-redux --save Redux Store Set Up We will create our store within our App.js file.
  • 21. // App.js import React, { Component } from 'react'; import { Provider } from 'react-redux'; import { createStore } from 'redux'; import totalReducers from './reducers/index.js'; import Counter from './components/counters'; const store = createStore(totalReducers); export default class App extends Component{ render(){ return( ); } }
  • 22. Here we are importing totalReducers from the reducers folder. createStore() function accepts one argument as totalReducers object and generates the store. The Provider component makes sure the store is available throughout the application. Reducers Set Up Reducers return the data required by the application. In this demo, the reducer will return the updated counter value. Here is our counterReducer.js file inside the reducers folder.
  • 23. // reducers/counterReducer.js let count= 0; export default (state = count , action) => { switch (action.type) { case "Increment": count ++ break; case "Decrement": count -- break; default: count; } return count; }
  • 24. The reducer defined above will always return the count value. Increment & Decrement are the actions types that will update the value as shown above. We will combine all the reducers inside the index.js file inside the reducers folder. Explanation // reducers/index.js import {combineReducers} from 'redux'; import counterReducer from './counterReducer'; const totalReducers= combineReducers({ count: counterReducer, }); export default totalReducers;
  • 25. Create two actions: Increment & Decrement. // actions/index.js export function increment(){ return{ type: "Increment" }; } export function decrement(){ return{ type: "Decrement" }; } Here we will combine all the reducers as arguments of the combineReducers() function of the Redux library. Explanation Actions Set Up
  • 26. mapStateToProps() – It simply accepts your reducer data, and converts it into a simple usable prop. With the help of this.props.data we will use data as a prop in our component. We will simply create only one component called a counter component. For using reducers and actions, we have to implement these functions: function mapStateToProps(state){ return{ count: state.count }; } UI Component
  • 27. mapDispatchToProps() – It accepts your actions, and converts them into a simple usable prop. Note: Keep in mind how we assigned names to reducers in the combineReducers function because we have to use the same name for calling respective reducers. function mapDispatchToProps(dispatch){ return bindActionCreators({increment, decrement}, dispatch) }
  • 28. Note: bindActionCreators function simply combines our actions into one object. Moving towards the component that manages the user interface. // component/counter.js class Counters extends Component { constructor(props) { super(props); } render() { return ( {'The Redux Approach'} {this.props.count} this.props.increment()}> Increment + this.props.decrement()}> Decrement -
  • 29. ); } } function mapStateToProps(state) { return { count: state.count, }; } function mapDispatchToProps(dispatch) { return bindActionCreators({increment, decrement}, dispatch); } export default connect(mapStateToProps, mapDispatchToProps)(Counters); For styling your component you can use the below code
  • 30. const styles = StyleSheet.create({ mainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, counterNumber: { fontSize: 35, fontWeight: 'bold', }, buttonContainer: { flexDirection: 'row', }, buttonStyle: { backgroundColor: 'green', borderWidth: 1, height: 30, width: '25%', borderRadius: 5, justifyContent: 'center', alignItems: 'center', }, });
  • 31. Finishing up So far we are done with redux set up, logic, and user interface for the counter demo. Now, just a simple step remaining – import our App file in our index.js file. // index.js import {AppRegistry} from 'react-native'; import App from './src/App.js'; import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App); Git Repo link: https://github.com/sunil- bacancy/CounterDemo
  • 33. Since the context API is in-built functionality we don’t need to install third- party dependencies. Folder structure Create a folder src at the root of your app. Within the src folder, we have to create 3 folders that are reducers, components, state and one file App.js.
  • 34. Just like Redux, declare reducer with Context API. // reducers/globalReducer.js export default countReducer = (state , action) => { switch (action.type) { case "Increment": return{ ...state, counter: state.counter + 1, } case "Decrement": return{ ...state, counter: state.counter - 1, } default: return{ state } } } Reducer Set Up
  • 35. Create the context Create a context using createContext() and pass the initial state as arguments. You can also define without passing arguments. Define a function that will pass the data via Provider. useReducer() will take a reducer having default state, then return the updated value and dispatch the function. Inside the Provider function, use useReducer() with arguments- reducer and initial state. The returned and dispatched states are later passed as values to the Provider. // state/globalState.js
  • 36. import React, {createContext, useReducer} from 'react'; import countReducer from '../reducers/globalReducer'; const initialState = { counter: 0 } export const GlobalContext = createContext(initialState); export default GlobalProvider = ({children}) => { const [state, dispatch] = useReducer(countReducer, initialState); return( {children} ) }
  • 37. Providing Context After creating the context, we need to provide the context so that it is accessible in the child components. For that, you need to wrap it within the Provider. // src/App.js import React, { Component } from 'react'; import GlobalProvider from './state/globalState'; import Counters from './components/counter'; export default class App extends Component { render(){ return ( ) } }
  • 38. Consuming Context Use useContext() for consuming the context in respective child components. // components/counter.js import React, { Component, useContext } from 'react'; import { View, Text, TouchableOpacity, StyleSheet } from 'react-native'; import {GlobalContext} from '../state/globalState'; const Counters = () => { const {state} = useContext(GlobalContext); const {dispatch} = useContext(GlobalContext); return(
  • 39. {'The Context API Approach' } { state.counter } dispatch({type: "Increment"})} > Increment + dispatch({type: "Decrement"})} > Decrement - ) } export default Counters; For styling your component you can use the below code.
  • 40. const styles = StyleSheet.create({ mainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center' }, counterNumber: { fontSize: 35, fontWeight: 'bold' }, buttonContainer: { flexDirection: 'row' }, buttonStyle: { backgroundColor: 'green', borderWidth: 1, height: 30, width: '25%', borderRadius: 5, justifyContent: 'center', alignItems: 'center', }, })
  • 41. Git Repo link: https://github.com/sunil- bacancy/CounterContextDemo
  • 43. Redux Storing and managing values Works outside React components Avoids prop-drilling Through dispatching an action one can update the value Provides DevTools to show history of actions and state values Allows application code for triggering side effects through middlewares Context API Not for storing or managing values Works in React components only Passes a single value be it objects, primitives, classes, etc. Avoids prop-drilling Provides current context value for Provider and Consumer but doesn’t display any history of how the value is changed. Excludes side effects mechanism – exclusively for component rendering
  • 45. I hope the tutorial helped you understand how different Context and Redux are. Moreover, try to implement both the approaches and play around with the code to dig deeper. For more such React Native tutorials, we have a React Native Tutorials page that consists of step-by-step guidelines with the github source. I can understand how difficult it can be to organize and manage a global store for a complex application, and also how precise you need to be for implementing Context API in your application. In case you have a large and complex project and want to implement Redux or Context API then feel free to contact us and hire React Native developer.