Zustand helps you manage app data simply and fast without extra complexity.
Zustand as lightweight alternative in React Native
Introduction
Syntax
React Native
import create from 'zustand' const useStore = create(set => ({ count: 0, increase: () => set(state => ({ count: state.count + 1 })), decrease: () => set(state => ({ count: state.count - 1 })) }))
create makes a store to hold your app data.
Inside, you define state variables and functions to change them.
Examples
React Native
const useStore = create(set => ({ name: 'Guest', setName: (newName) => set({ name: newName }) }))
React Native
const useStore = create(set => ({ todos: [], addTodo: (todo) => set(state => ({ todos: [...state.todos, todo] })) }))
Sample App
This React Native app shows a number and two buttons to add or subtract from it. Zustand keeps the number state simple and shared inside the app.
React Native
import React from 'react' import { View, Text, Button } from 'react-native' import create from 'zustand' const useStore = create(set => ({ count: 0, increase: () => set(state => ({ count: state.count + 1 })), decrease: () => set(state => ({ count: state.count - 1 })) })) export default function Counter() { const { count, increase, decrease } = useStore() return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text style={{ fontSize: 48, marginBottom: 20 }} accessibilityLabel="Current count">{count}</Text> <View style={{ flexDirection: 'row', gap: 20 }}> <Button title="-" onPress={decrease} accessibilityLabel="Decrease count" /> <Button title="+" onPress={increase} accessibilityLabel="Increase count" /> </View> </View> ) }
Important Notes
Zustand is very small and fast, so it won't slow your app.
You can use Zustand with React Native without extra setup like Redux.
Remember to keep your state simple and only store what you need.
Summary
Zustand is a simple tool to manage app data in React Native.
It uses a store with state and functions to update it.
It helps keep your app code clean and easy to maintain.
Practice
1. What is the main purpose of Zustand in React Native apps?
easy
Solution
Step 1: Understand Zustand's role
Zustand is designed to manage state, which means keeping track of data that changes in the app.Step 2: Compare options to Zustand's purpose
Options B, C, and D relate to animations, network, and styling, which are not Zustand's focus.Final Answer:
To manage app state in a simple and lightweight way -> Option AQuick Check:
Zustand = simple state management [OK]
Hint: Zustand is about state, not styling or animations [OK]
Common Mistakes:
- Confusing Zustand with animation libraries
- Thinking Zustand handles network calls
- Assuming Zustand is for styling
2. Which of the following is the correct way to create a Zustand store in React Native?
easy
Solution
Step 1: Recall Zustand store syntax
Zustand uses the create function with a callback that receives set to update state.Step 2: Check each option's syntax
const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) })) matches Zustand's pattern with create and set. Options A, B, and D use incorrect functions or syntax.Final Answer:
const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) })) -> Option BQuick Check:
Use create with set callback = const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) })) [OK]
Hint: Zustand store uses create() with set callback [OK]
Common Mistakes:
- Using createStore instead of create
- Not passing set function
- Incorrect increment function syntax
3. Given this Zustand store code:
What will be displayed when
const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) }))
function Counter() {
const count = useStore(state => state.count)
const increment = useStore(state => state.increment)
increment()
return {count}
}What will be displayed when
Counter renders?medium
Solution
Step 1: Understand state updates in render
The increment function is called inside the component body, but React state updates are asynchronous and won't reflect immediately in the same render.Step 2: Determine displayed count value
Since increment updates state after render, the displayed count remains the initial 0 during this render.Final Answer:
0 -> Option CQuick Check:
State updates async, initial render shows 0 [OK]
Hint: State changes don't show immediately in same render [OK]
Common Mistakes:
- Assuming increment updates count instantly
- Expecting count to be 1 immediately
- Confusing state update timing
4. Identify the error in this Zustand store code:
const useStore = create(set => ({
count: 0,
increment: () => set({ count: count + 1 })
}))medium
Solution
Step 1: Check how count is used in set
The increment function uses count directly, but count is not defined in that scope; it should access state.count.Step 2: Correct usage of set with state
set should receive a function with state parameter to get current count: set(state => ({ count: state.count + 1 })).Final Answer:
Using count without accessing state inside set -> Option AQuick Check:
Access state.count inside set callback [OK]
Hint: Use set(state => ...) to access current state [OK]
Common Mistakes:
- Using count variable directly instead of state.count
- Not passing function to set
- Ignoring state parameter in set callback
5. You want to add a reset function to this Zustand store that sets count back to zero. Which is the correct way?
const useStore = create(set => ({
count: 0,
increment: () => set(state => ({ count: state.count + 1 })),
reset: ???
}))hard
Solution
Step 1: Understand reset goal
The reset function should set count directly to zero, ignoring current state.Step 2: Evaluate each option
() => set(state => ({ count: null })) sets count to null (wrong). Options B and C referencestate.countdirectly inset({ ... })butstateis undefined there (error). () => set({ count: 0 }) correctly sets{ count: 0 }directly.Final Answer:
() => set({ count: 0 }) -> Option DQuick Check:
Reset sets count directly to zero [OK]
Hint: Reset sets count directly to 0 without state calculation [OK]
Common Mistakes:
- Using calculations instead of direct assignment
- Setting count to null instead of zero
- Overcomplicating reset function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
