Skip to main content

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.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Zustand Auth Store with SecureStore

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

Module Quiz
Question 1 of 5

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.

Diagram
Loading diagramโ€ฆ

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.

Topics in This Module

0/9 done