state-management-in-react-native
state-management-in-react-native
• When our component function runs, the callback will be called if any
dependencies have changed since the last time the component
function ran.
• useEffect(function, [dependencies])
• 1. useEffect( ( ) => { } ) Runs after every re-render
• 2. useEffect( ( ) => { } , [ ] ) Runs only on mount
• 3. useEffect( ( ) => { } , [value] ) Runs only on mount + when value changes
No dependencies
Every time this
component re-
renders,
including
initially when
we mount the
component,
we will
perform the
code inside
useEffect()
Empty
Dependency
array
Only appears
once the
component
renders
Updater function
By naming convention
Updater function
ChildA
ChildB
ChildC
ChildD
Set state variable and setter
ChildA
If we were using props…
ChildA
If we were using props…
ChildB
ChildC
If we were using props…
ChildD
Prop drilling
• the practice of passing data (props) down through multiple levels of components in a
component tree, even when some components don't directly use that data, just to reach
a deeply nested child component that needs it
• Imagine a parent component needs to pass data to a grandchild component, but
there's a child component in between. Instead of passing the data directly from
the parent to the grandchild, you pass the data from the parent to the child, and
then the child passes it down to the grandchild, even if the child component
doesn't use the data itself.
• Code that's harder to maintain and understand - Intermediate components
become "prop passers" and their purpose becomes unclear.
• Increased cognitive load for developers - It can be harder to follow the flow of data
through the component tree.
• Potential for unnecessary re-renders - If a component is passed a prop it doesn't
use, but that prop changes, the component will still re-render.
useContext
• React Hook that allows you to share values
between multiple levels of components without
passing props through each level
• Consumer component
• Provider component • import {useContext} from ‘react’;
• import {createContext} from ‘react’; • import {MyContext} from “@/ChildA”
• export const MyContext = createContext();
• const value =
• <MyContext.Provider value={value}> useContext(MyContext)
<Child />
</MyContext.Provider>
ChildA
ChildD
ChildC
Counter program
• Add, subtract, reset, change color
• Alternate between at least 5 different colors
• Use updater functions least 5 times each
• Terminal should log both the current number and the color
Dice roller program
• Button to roll two numbers randomly each between 1-6
• Roll value 1-2 = text color #1
• Roll value 3-4 = text color #2
• Roll value 5-6 = text color #3
• Terminal should log both the current numbers and their
respective colors
STATE
MANAGEMENT
IN
REACT NATIVE
18
WHAT IS REDUX ?
Redux is a predictable state container for JavaScript apps (including React
& React Native).
GROUP 1
19
CORE CONCEPTS
Store
Actions
GROUP 1
20
CORE CONCEPTS
Reducers Data Flow (Unidirectional)
GROUP 1
EX: TODO APP 21
Action Store
Reducer
22
WHY REDUX TOOLKIT ?
Redux Toolkit (RTK) is the official, recommended way to write Redux logic.
GROUP 1
23
KEY FEATURES
createSlice
GROUP 1
23
KEY FEATURES
configureStore
GROUP 1
24
KEY FEATURES
Simplified Reducers
Uses Immer under the hood so you can write
“mutating” code that is actually immutable.
No need for deep copying or manually managing
immutability.
GROUP 1
25
COMPARISON WITH TRADITIONAL REDUX
WHAT IS RECOIL AND RECOIL ROOT
ATOMS SELECTOR
WHAT IS A ZUSTAND
Consider Recoil if: You have a large, complex application where fine-grained
updates and a structured approach to state derivation are crucial for
performance and maintainability. The built-in debugging tools can also be a
big plus.
Consider Zustand if: You prefer a simpler, more lightweight solution with a
minimal API. It's excellent for projects of all sizes where you want to get up
and running quickly without a lot of overhead.
Best Practices & When to Use What