Lesson 9 of 9
Practice & Quiz - State Management in React Native
Test your knowledge of Zustand with AsyncStorage persistence, SecureStore for JWT tokens, and auth state patterns with a practical exercise and 5 quiz questions.
Practical Exercise - Zustand Auth Store with SecureStore
import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-async-storage/async-storage';
// โโโ SecureStore adapter for Zustand persist โโโโโโโโโโโโโโโโโโ
// SecureStore is encrypted - use it for sensitive data (tokens)
const secureStorage = {
getItem: async (key: string) => {
const value = await SecureStore.getItemAsync(key);
return value ?? null;
},
setItem: (key: string, value: string) =>
SecureStore.setItemAsync(key, value),
removeItem: (key: string) =>
SecureStore.deleteItemAsync(key),
};
// โโโ Auth store - sensitive data in SecureStore โโโโโโโโโโโโโโโ
interface User { id: string; name: string; email: string; }
interface AuthStore {
user: User | null;
accessToken: string | null;
isAuthenticated: boolean;
login: (user: User, token: string) => void;
logout: () => void;
updateToken: (token: string) => void;
}
export const useAuthStore = create<AuthStore>()(
persist(
(set) => ({
user: null,
accessToken: null,
isAuthenticated: false,
login: (user, accessToken) =>
set({ user, accessToken, isAuthenticated: true }),
logout: () =>
set({ user: null, accessToken: null, isAuthenticated: false }),
updateToken: (accessToken) => set({ accessToken }),
}),
{
name: 'auth-store',
storage: createJSONStorage(() => secureStorage), // encrypted on device
// Only persist non-sensitive fields to SecureStore; token is persisted too
// For truly sensitive apps, skip persisting accessToken and re-auth on restart
}
)
);
// โโโ App preferences - non-sensitive, AsyncStorage is fine โโโโ
interface PrefsStore {
theme: 'light' | 'dark' | 'system';
language: string;
notificationsEnabled: boolean;
setTheme: (theme: PrefsStore['theme']) => void;
setLanguage: (language: string) => void;
}
export const usePrefsStore = create<PrefsStore>()(
persist(
(set) => ({
theme: 'system',
language: 'en',
notificationsEnabled: true,
setTheme: (theme) => set({ theme }),
setLanguage: (language) => set({ language }),
}),
{
name: 'prefs-store',
storage: createJSONStorage(() => AsyncStorage),
}
)
);
// โโโ Usage in a component โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function Header() {
const { user, isAuthenticated, logout } = useAuthStore();
const { theme } = usePrefsStore();
if (!isAuthenticated) return null;
return (
<View style={{ flexDirection: 'row', justifyContent: 'space-between', padding: 16 }}>
<Text>Hello, {user?.name} ({theme} mode)</Text>
<Pressable onPress={logout}><Text>Logout</Text></Pressable>
</View>
);
}Module Quiz
What is the key security difference between AsyncStorage and Expo SecureStore?
Tip
Tip
Practice Practice Quiz State Management in React Native in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Module 6 Knowledge Check: Zustand, Redux Toolkit, MMKV, and secure storage
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz State Management in React Native from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz State Management in React Native is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?