Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to the third lesson in our series on Zustand.
In our last session, we compared Zustand with Redux and MobX. We concluded that Zustand offers a compelling middle ground, providing the global state capabilities of Redux without the boilerplate, and achieving high performance similar to MobX but with a simpler, hooks-native API.
Today, we move from theory to practice. The goal is to create a clean, working environment where we can experiment with Zustand's features throughout the rest of this course.
Learning Outcome: By the end of this lesson, you will be able to set up a minimal React project with Zustand to serve as a sandbox environment.
1. Scaffolding Your React Project with Vite
For our sandbox, we'll use Vite to scaffold the React application. Given your experience, you're likely familiar with create-react-app, but Vite has become a standard for modern front-end development due to its extremely fast Hot Module Replacement (HMR) and efficient build process, which leverages native ES modules.
The following video provides a concise walkthrough of the initial setup process.
Zustand React State Management Course (Simple Redux Alternative)
This clip from the freeCodeCamp.org course on Zustand will guide you through creating a new React project with Vite and installing the Zustand dependency.
Watch from 07:25 to 08:12. Follow the steps to initialize the project and install the dependencies. We will handle running the dev server in a later step.
To summarize and formalize the steps, please execute the following commands in your terminal in an empty directory:
-
Create the Vite project:
npm create vite@latestWhen prompted:
- Enter a project name (e.g.,
zustand-sandbox). - Select
React. - Select
JavaScript. (While the video uses JavaScript, you could also selectTypeScriptgiven your background. Thedev.toarticle we'll see next uses TypeScript, so you can refer to it for type definitions if you choose that path.)
- Enter a project name (e.g.,
-
Navigate into your new project directory:
cd zustand-sandbox -
Install the project dependencies:
npm install -
Install Zustand:
npm install zustand
You now have a basic React project with Zustand added as a dependency.
2. Structuring and Cleaning the Sandbox
A clean starting point is essential for focused learning. The default Vite template includes some boilerplate code (logos, CSS, and a counter example) that we should remove. We'll also establish a simple folder structure for our state management code.
The article "Using Zustand with React JS!" provides a good textual reference for this process and introduces a conventional project structure.
This article from dev.to mirrors the setup process and also suggests a good practice for organizing store files.
Skim the sections 'Creating the project' and the beginning of 'Creating a store'. Note the commands, which should be familiar from the video. Pay particular attention to the recommendation of creating a src/store folder. We will follow this convention.
Let's apply these ideas to clean up our project:
-
Create a store directory: Inside the
srcfolder, create a new folder namedstore. This is where we will keep all our Zustand store definitions. -
Clean up
src/App.css: Open this file and delete all of its content. -
Clean up
src/index.css: Open this file and delete all of its content. We can add minimal global styles later if needed. -
Clean up
src/App.jsx: Replace the entire content ofApp.jsxwith the following minimal component:function App() { return ( <div> <h1>Zustand Sandbox</h1> </div> ) } export default App -
Delete unused assets: You can now safely delete the
src/assetsfolder, as thereact.svglogo is no longer referenced.
3. Verifying Your Sandbox Environment
With the project created, dependencies installed, and boilerplate removed, the final step is to ensure everything is working correctly.
Run the development server from your project's root directory:
npm run dev
Open your browser and navigate to the local URL provided by Vite (usually http://localhost:5173). You should see a simple page with the heading "Zustand Sandbox".
Your project structure should now look something like this:
zustand-sandbox/
āāā node_modules/
āāā public/
ā āāā vite.svg
āāā src/
ā āāā store/
ā āāā App.jsx
ā āāā main.jsx
āāā .eslintrc.cjs
āāā .gitignore
āāā index.html
āāā package.json
āāā package-lock.json
āāā vite.config.js
You have successfully created a clean sandbox environment. This setup is the foundation upon which we will build in the upcoming lessons.
Conclusion
In this lesson, you have:
- Scaffolded a new React project using the modern toolchain provided by Vite.
- Installed
zustandas a project dependency. - Cleaned the default project template and established a simple, scalable folder structure by creating a
src/storedirectory. - Verified that the minimal application is running correctly.
You are now fully prepared to start building with Zustand.
Next Lesson Preview:
In our next lesson, we will use this sandbox to create a basic store using the create function, defining both state and actions. We'll write our first lines of Zustand code and see how it enables us to manage state outside of our React components.