Bird
Raised Fist0
React Nativemobile~20 mins

Zustand as lightweight alternative in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Counter with Zustand
A simple counter app screen using Zustand for state management instead of React's useState.
Target UI
Counter with Zustand

+---------------------+
|       Counter       |
|                     |
|         0           |
|                     |
|  [+]       [-]      |
+---------------------+
Use Zustand to create a global store with a count state and increment/decrement actions.
Display the current count in the center.
Add two buttons: '+' to increase count, '-' to decrease count.
Buttons update the count using Zustand store actions.
UI updates automatically when count changes.
Starter Code
React Native
import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import create from 'zustand';

// TODO: Create Zustand store here

export default function CounterScreen() {
  // TODO: Use Zustand store here

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Counter with Zustand</Text>
      <Text style={styles.count}>0</Text>
      <View style={styles.buttons}>
        <Button title="+" onPress={() => {}} />
        <Button title="-" onPress={() => {}} />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  title: { fontSize: 24, marginBottom: 20 },
  count: { fontSize: 48, marginBottom: 20 },
  buttons: { flexDirection: 'row', width: '40%', justifyContent: 'space-between' }
});
Task 1
Task 2
Task 3
Task 4
Solution
React Native
import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import create from 'zustand';

const useStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 })),
  decrement: () => set(state => ({ count: state.count - 1 }))
}));

export default function CounterScreen() {
  const { count, increment, decrement } = useStore();

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Counter with Zustand</Text>
      <Text style={styles.count}>{count}</Text>
      <View style={styles.buttons}>
        <Button title="+" onPress={increment} />
        <Button title="-" onPress={decrement} />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  title: { fontSize: 24, marginBottom: 20 },
  count: { fontSize: 48, marginBottom: 20 },
  buttons: { flexDirection: 'row', width: '40%', justifyContent: 'space-between' }
});

We created a Zustand store using create with a count state and two actions: increment and decrement. These actions update the count by adding or subtracting 1.

In the CounterScreen component, we use the store by calling useStore() to get the current count and the two actions.

The UI shows the count in a large font. The '+' and '-' buttons call the increment and decrement actions respectively when pressed.

This setup keeps state management simple and global without needing React's useState or context, demonstrating Zustand as a lightweight alternative.

Final Result
Completed Screen
Counter with Zustand

+---------------------+
|       Counter       |
|                     |
|         5           |
|                     |
|  [+]       [-]      |
+---------------------+
Tapping '+' increases the number shown by 1.
Tapping '-' decreases the number shown by 1.
The number updates immediately on screen.
Stretch Goal
Add a reset button that sets the count back to zero.
💡 Hint
Add a 'reset' action in the Zustand store and a new Button that calls it.

Practice

(1/5)
1. What is the main purpose of Zustand in React Native apps?
easy
A. To manage app state in a simple and lightweight way
B. To create complex animations
C. To handle network requests
D. To style components with CSS

Solution

  1. Step 1: Understand Zustand's role

    Zustand is designed to manage state, which means keeping track of data that changes in the app.
  2. 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.
  3. Final Answer:

    To manage app state in a simple and lightweight way -> Option A
  4. Quick 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
A. const useStore = new Store({ count: 0, increment: () => count++ })
B. const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) }))
C. const useStore = createStore({ count: 0, increment() { count++ } })
D. const useStore = createStore(() => ({ count: 0, increment: () => count++ }))

Solution

  1. Step 1: Recall Zustand store syntax

    Zustand uses the create function with a callback that receives set to update state.
  2. 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.
  3. Final Answer:

    const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) })) -> Option B
  4. Quick 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:
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
A. undefined
B. 1
C. 0
D. Error

Solution

  1. 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.
  2. Step 2: Determine displayed count value

    Since increment updates state after render, the displayed count remains the initial 0 during this render.
  3. Final Answer:

    0 -> Option C
  4. Quick 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
A. Using count without accessing state inside set
B. Missing initial count value
C. Using arrow function incorrectly
D. Not returning an object from create

Solution

  1. 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.
  2. 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 })).
  3. Final Answer:

    Using count without accessing state inside set -> Option A
  4. Quick 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
A. () => set(state => ({ count: null }))
B. () => set({ count: state.count - state.count })
C. () => set({ count: state.count * 0 })
D. () => set({ count: 0 })

Solution

  1. Step 1: Understand reset goal

    The reset function should set count directly to zero, ignoring current state.
  2. Step 2: Evaluate each option

    () => set(state => ({ count: null })) sets count to null (wrong). Options B and C reference state.count directly in set({ ... }) but state is undefined there (error). () => set({ count: 0 }) correctly sets { count: 0 } directly.
  3. Final Answer:

    () => set({ count: 0 }) -> Option D
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard