SlideShare a Scribd company logo
How to Use
Redux with
React Hooks in
React Native
Application
w w w . b a c a n c y t e c h n o l o g y . c o m
The react-redux library has official support
for the Hooks in React as well as React
Native applications to use Redux as a state
management library. With more and more
adoption of React Hooks and its ability to
handle side effects and components, now, it
is regarded as a standard functional
component pattern.
Redux hooks are available since the release
of React version 16.8.x, and in this blog post,
we will explore a structured pattern for
Redux making use of React Native.
Hooks for
React-Redux:
To replace the higher-order component
Redux hook API is used, “connect()” with
hooks like “useDispatch” and “useSelector”
as present React-Redux provides 3 hooks:
⦿useSelector():
This can help you replace the
mapStateToProps. The purpose is to extract
the data from the ‘Redux’ store whenever
function component renders.
⦿useDispatch():
A replacement to mapDispatchToProps, it’s
purpose is to return the reference to the
dispatch object.
⦿useStore():
It helps to return the reference that was
wrapped in Redux . It is ideal to use in
specific scenarios, like replacing reducers.
"dependencies": {
"@react-native-community/masked-view":
"0.1.5",
"expo": "~36.0.0",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native":
"https://github.com/expo/react-
native/archive/sdk-36.0.0.tar.gz",
"react-native-gesture-handler": "~1.5.0",
"react-native-paper": "3.4.0",
"react-native-reanimated": "~1.4.0",
"react-native-safe-area-context": "0.6.0",
"react-native-screens": "2.0.0-alpha.12",
"react-navigation": "4.0.10",
"react-navigation-stack": "2.0.10",
"react-redux": "7.1.3",
"redux": "4.0.5"
},
Source: Heartbeat
Let’s Start with Installing
Redux
The very next step is to install the
dependencies to use Redux and manage the
state:
yarn add redux react-redux lodash.remove
Here I am going to follow ducks for
directory structure to manage Redux files
as ducks let you have modular reducers in
the application itself. There is no need to
create separate files for types, actions, and
action creators. All of these can be managed
in one modular file, and if it demands to
create more than one reducer, then you can
simply define multiple reducer files.
How to Add Action Types and Creators
While using Redux, the state is
characterized by JS object. It would be best
if you considered an object as ready as it
does not allow you to make changes in the
state. This is the reason you need to take the
help of actions.
Actions are similar to the events in Redux.
To start with
src/ directory
Create subdirectory – Redux
Create a new file - notesApp.js.
Now you have provided an additional ability
to allow users to add notes.
// Action Types
export const ADD_NOTE = 'ADD_NOTE'
export const DELETE_NOTE =
'DELETE_NOTE'
To Create Reducers
Path: src / store /
File: /ducks/example.js
The receiver of the action is addressed as a
reducer; whenever an action takes place,
the state of the application changes and
the app’s state is done by reducers.
Reducer functions based on the previous
and initial state.
Let’s Move Ahead to Add a
Reducer
// import the dependency
import remove from 'lodash.remove'
// reducer
const initialState = []
function notesReducer(state = initialState, action) {
switch (action.type) {
case ADD_NOTE:
return [
...state,
{
id: action.id,
note: action.note
}
]
case DELETE_NOTE:
const deletedNewArray = remove(state, obj => {
return obj.id != action.payload
})
return deletedNewArray
default:
return state
}
}
export default notesReducer
Source: Viblo
Holds application state
Allows access to the state via getState()
Let the state be updated
via dispatch(action).
Provides permission to registers
listeners via subscribe(listener)
Basically, the Redux store is responsible
for the following:
Under the src folder, create a store.js file
and configure it with the Redux store to
define the initialState parameter.
Redux Store
/*
* src/store.js
* No initialState
*/import { createStore, applyMiddleware }
from 'redux';
import thunk from 'redux-thunk';
import rootReducer from
'./reducers/rootReducer';export default
function configureStore() {
return createStore(
rootReducer,
applyMiddleware(thunk)
);
}
Source: Medium
The Redux store is set up, but the
application has no access to it. Using a
provider from React binding react-redux,
the store will be available to every
component in the application. Here I am
going to consider the store and children as
props.
To Create Store
/*
src/index.js
*/import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'
import configureStore from './store';import
'./index.css';
import App from './App';
import registerServiceWorker from
'./registerServiceWorker';ReactDOM.render
(
< Provider store={configureStore()} >
< App />
< /Provider >,
document.getElementById('root')
);
registerServiceWorker();
Source: Medium
To access state with Redux, use the
useSelector, it is very similar to the
mapStateToProps inside connect(). It
allows you to extract data from the Redux
store state using a selector function.
The significant difference between the
argument and the Hook is the selector will
present any value in return not any object
as a result.
Simply open the ViewNotes.js file.
// ...after rest of the imports
import { useSelector } from 'react-redux'
To Access the Global State
The Hook is only used to perform to
dispatch function from the Redux store.
Simply import it from the react-redux
along with the action creators to dispatch
an action.
import { useSelector, useDispatch } from
'react-redux'
To dispatch an action define the following
statement useSelectorHook:
const dispatch = useDispatch()
Dispatching Actions
const addNote = note =>
dispatch(addnote(note))
const deleteNote = id =>
dispatch(deletenote(id))
FYI, here’s a code snipet of List.Item
< List.Item
title={item.note.noteTitle}
description={item.note.noteValue}
descriptionNumberOfLines={1}
titleStyle={styles.listTitle}
onPress={() => deleteNote(item.id)}
/ >
To trigger these events:
So far, you are good to go with running an
application from the terminal window
executing the command called expo start.
For your reference, here’s a complete
snippet of code.
To Run the Application
How to use redux with react hooks in react native application
The additional hooks such as useDispatch
and useSelector not only eliminates the
need to write ample of boilerplate code but
also provides the additional advantages of
using functional components. To
understand more in detail, please refer to
their official document. If you are planning
to hire React Native developers, with the
relevant skillset who can help you
implement Hooks in your React-Redux
application, then you have landed on the
right page.
We are a globally renowned React Native
development company and can help you
build visually stunning and future-proof
mobile app solutions.
Wrapping Up
Thank you

More Related Content

PPTX
Damian Kmiecik - Road trip with Redux
PDF
Manage the Flux of your Web Application: Let's Redux
PPTX
PDF
React&redux
PDF
Droidjam 2019 flutter isolates pdf
PDF
Intro to Redux | DreamLab Academy #3
PDF
Testowanie JavaScript
PDF
React + Redux. Best practices
Damian Kmiecik - Road trip with Redux
Manage the Flux of your Web Application: Let's Redux
React&redux
Droidjam 2019 flutter isolates pdf
Intro to Redux | DreamLab Academy #3
Testowanie JavaScript
React + Redux. Best practices

What's hot (20)

DOCX
MVest Spring Job Execution
PPTX
React with Redux
PDF
React redux
PDF
React lecture
PPTX
Rxjs swetugg
PPTX
Google apps script database abstraction exposed version
PDF
Advanced Akka For Architects
PDF
Workshop 20: ReactJS Part II Flux Pattern & Redux
PPTX
Redux training
PDF
iOS for ERREST - alternative version
PDF
React 101
PPTX
Rxjs marble-testing
PDF
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
PPTX
Dbabstraction
PDF
React & Redux
PPT
Ext J S Observable
PPT
Ext Js Events
PPTX
Google cloud datastore driver for Google Apps Script DB abstraction
PPTX
Client-side Rendering with AngularJS
PPTX
20141001 delapsley-oc-openstack-final
MVest Spring Job Execution
React with Redux
React redux
React lecture
Rxjs swetugg
Google apps script database abstraction exposed version
Advanced Akka For Architects
Workshop 20: ReactJS Part II Flux Pattern & Redux
Redux training
iOS for ERREST - alternative version
React 101
Rxjs marble-testing
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Dbabstraction
React & Redux
Ext J S Observable
Ext Js Events
Google cloud datastore driver for Google Apps Script DB abstraction
Client-side Rendering with AngularJS
20141001 delapsley-oc-openstack-final
Ad

Similar to How to use redux with react hooks in react native application (20)

PDF
Materi Modern React Redux Power Point.pdf
PPTX
React js programming concept
PPTX
Getting started with Redux js
PDF
Reactивная тяга
PDF
Simple React Todo List
PDF
A Blog About Using State in Next.js: Valuable Insights
PDF
React js
PDF
A full introductory guide to React
PPTX
React + Redux + TypeScript === ♥
PDF
Redux and context api with react native app introduction, use cases, implemen...
PPTX
React Hooks Best Practices in 2022.pptx
PDF
Reactive.architecture.with.Angular
PDF
Fundamental concepts of react js
PPTX
U3-02-React Redux and MUI.pptxaSDFGNXDASDFG
PPTX
downloads_introduction to redux.pptx
PPTX
React - Start learning today
PPTX
React outbox
PPTX
Introduction to React JS for beginners
PDF
React Redux Interview Questions PDF By ScholarHat
Materi Modern React Redux Power Point.pdf
React js programming concept
Getting started with Redux js
Reactивная тяга
Simple React Todo List
A Blog About Using State in Next.js: Valuable Insights
React js
A full introductory guide to React
React + Redux + TypeScript === ♥
Redux and context api with react native app introduction, use cases, implemen...
React Hooks Best Practices in 2022.pptx
Reactive.architecture.with.Angular
Fundamental concepts of react js
U3-02-React Redux and MUI.pptxaSDFGNXDASDFG
downloads_introduction to redux.pptx
React - Start learning today
React outbox
Introduction to React JS for beginners
React Redux Interview Questions PDF By ScholarHat
Ad

More from Katy Slemon (20)

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

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Weekly Chronicles - August'25 Week I
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Modernizing your data center with Dell and AMD
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Approach and Philosophy of On baking technology
NewMind AI Monthly Chronicles - July 2025
Spectral efficient network and resource selection model in 5G networks
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

How to use redux with react hooks in react native application

  • 1. How to Use Redux with React Hooks in React Native Application w w w . b a c a n c y t e c h n o l o g y . c o m
  • 2. The react-redux library has official support for the Hooks in React as well as React Native applications to use Redux as a state management library. With more and more adoption of React Hooks and its ability to handle side effects and components, now, it is regarded as a standard functional component pattern. Redux hooks are available since the release of React version 16.8.x, and in this blog post, we will explore a structured pattern for Redux making use of React Native.
  • 4. To replace the higher-order component Redux hook API is used, “connect()” with hooks like “useDispatch” and “useSelector” as present React-Redux provides 3 hooks: ⦿useSelector(): This can help you replace the mapStateToProps. The purpose is to extract the data from the ‘Redux’ store whenever function component renders. ⦿useDispatch(): A replacement to mapDispatchToProps, it’s purpose is to return the reference to the dispatch object. ⦿useStore(): It helps to return the reference that was wrapped in Redux . It is ideal to use in specific scenarios, like replacing reducers.
  • 5. "dependencies": { "@react-native-community/masked-view": "0.1.5", "expo": "~36.0.0", "react": "~16.9.0", "react-dom": "~16.9.0", "react-native": "https://github.com/expo/react- native/archive/sdk-36.0.0.tar.gz", "react-native-gesture-handler": "~1.5.0", "react-native-paper": "3.4.0", "react-native-reanimated": "~1.4.0", "react-native-safe-area-context": "0.6.0", "react-native-screens": "2.0.0-alpha.12", "react-navigation": "4.0.10", "react-navigation-stack": "2.0.10", "react-redux": "7.1.3", "redux": "4.0.5" }, Source: Heartbeat Let’s Start with Installing Redux
  • 6. The very next step is to install the dependencies to use Redux and manage the state: yarn add redux react-redux lodash.remove Here I am going to follow ducks for directory structure to manage Redux files as ducks let you have modular reducers in the application itself. There is no need to create separate files for types, actions, and action creators. All of these can be managed in one modular file, and if it demands to create more than one reducer, then you can simply define multiple reducer files. How to Add Action Types and Creators
  • 7. While using Redux, the state is characterized by JS object. It would be best if you considered an object as ready as it does not allow you to make changes in the state. This is the reason you need to take the help of actions. Actions are similar to the events in Redux. To start with src/ directory Create subdirectory – Redux Create a new file - notesApp.js. Now you have provided an additional ability to allow users to add notes. // Action Types export const ADD_NOTE = 'ADD_NOTE' export const DELETE_NOTE = 'DELETE_NOTE'
  • 8. To Create Reducers Path: src / store / File: /ducks/example.js The receiver of the action is addressed as a reducer; whenever an action takes place, the state of the application changes and the app’s state is done by reducers. Reducer functions based on the previous and initial state. Let’s Move Ahead to Add a Reducer
  • 9. // import the dependency import remove from 'lodash.remove' // reducer const initialState = [] function notesReducer(state = initialState, action) { switch (action.type) { case ADD_NOTE: return [ ...state, { id: action.id, note: action.note } ] case DELETE_NOTE: const deletedNewArray = remove(state, obj => { return obj.id != action.payload }) return deletedNewArray default: return state } } export default notesReducer Source: Viblo
  • 10. Holds application state Allows access to the state via getState() Let the state be updated via dispatch(action). Provides permission to registers listeners via subscribe(listener) Basically, the Redux store is responsible for the following: Under the src folder, create a store.js file and configure it with the Redux store to define the initialState parameter. Redux Store
  • 11. /* * src/store.js * No initialState */import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from './reducers/rootReducer';export default function configureStore() { return createStore( rootReducer, applyMiddleware(thunk) ); } Source: Medium
  • 12. The Redux store is set up, but the application has no access to it. Using a provider from React binding react-redux, the store will be available to every component in the application. Here I am going to consider the store and children as props. To Create Store
  • 13. /* src/index.js */import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux' import configureStore from './store';import './index.css'; import App from './App'; import registerServiceWorker from './registerServiceWorker';ReactDOM.render ( < Provider store={configureStore()} > < App /> < /Provider >, document.getElementById('root') ); registerServiceWorker(); Source: Medium
  • 14. To access state with Redux, use the useSelector, it is very similar to the mapStateToProps inside connect(). It allows you to extract data from the Redux store state using a selector function. The significant difference between the argument and the Hook is the selector will present any value in return not any object as a result. Simply open the ViewNotes.js file. // ...after rest of the imports import { useSelector } from 'react-redux' To Access the Global State
  • 15. The Hook is only used to perform to dispatch function from the Redux store. Simply import it from the react-redux along with the action creators to dispatch an action. import { useSelector, useDispatch } from 'react-redux' To dispatch an action define the following statement useSelectorHook: const dispatch = useDispatch() Dispatching Actions
  • 16. const addNote = note => dispatch(addnote(note)) const deleteNote = id => dispatch(deletenote(id)) FYI, here’s a code snipet of List.Item < List.Item title={item.note.noteTitle} description={item.note.noteValue} descriptionNumberOfLines={1} titleStyle={styles.listTitle} onPress={() => deleteNote(item.id)} / > To trigger these events:
  • 17. So far, you are good to go with running an application from the terminal window executing the command called expo start. For your reference, here’s a complete snippet of code. To Run the Application
  • 19. The additional hooks such as useDispatch and useSelector not only eliminates the need to write ample of boilerplate code but also provides the additional advantages of using functional components. To understand more in detail, please refer to their official document. If you are planning to hire React Native developers, with the relevant skillset who can help you implement Hooks in your React-Redux application, then you have landed on the right page. We are a globally renowned React Native development company and can help you build visually stunning and future-proof mobile app solutions. Wrapping Up