Skip to content

Run It on Your Computer

The Course
•
30 min study

The labs are convenient, but real projects live on your computer, in your own editor. In this lesson you'll run Study Tasks with Vite, then add something the lab's sandbox can't do: saving tasks between visits.

1. Install Node.js

Vite needs Node.js 20.19 or later, or 22.12 or later. Check what you have in a terminal:

Shell
node --version

If the command isn't found, or the version is older, install the current LTS release from nodejs.org. It includes npm.

2. Download and start the project

Press Download project under the Study Tasks lab in the previous lesson. Unzip study-tasks.zip, then open a terminal in the study-tasks folder:

Shell
npm install
Shell
npm run dev

npm install downloads React, Vite and the React plugin into node_modules. npm run dev starts a development server and prints an address, usually http://localhost:5173. Open it in your browser.

Leave the server running and open the folder in an editor such as VS Code. Change the heading in src/App.jsx and save. The browser updates straight away, keeping the tasks you've added. That's Vite's hot module replacement.

3. What's in the folder

FileWhat it does
index.htmlThe page the browser loads, with <div id="root">
src/Your code: the same files as the lab
package.jsonThe project's name, scripts (dev, build, preview) and dependencies
vite.config.jsTells Vite to use the React plugin, which compiles JSX
node_modules/Installed packages. Never edit it; npm install rebuilds it

npm run build writes a production version to dist/, ready to put on any web host.

4. Remember the tasks

Reload the page: your tasks are gone, because they only lived in state. The browser's localStorage can keep a little text for a site between visits. The lab's sandbox blocks it, which is why this step happens on your computer.

Create src/storage.js:

JavaScript
const KEY = "study-tasks";

export function loadTasks() {
  try {
    const saved = localStorage.getItem(KEY);
    if (saved === null) return null;
    const tasks = JSON.parse(saved);
    if (!Array.isArray(tasks)) return null;
    const ids = new Set();
    const valid = tasks.every((task) => {
      if (task === null || typeof task !== "object") return false;
      const validId = typeof task.id === "string" ||
        (typeof task.id === "number" && Number.isFinite(task.id));
      if (!validId || ids.has(String(task.id))) return false;
      ids.add(String(task.id));
      return typeof task.title === "string" && task.title.trim() !== "" &&
        Number.isInteger(task.minutes) && task.minutes > 0 &&
        typeof task.done === "boolean";
    });
    return valid ? tasks : null;
  } catch {
    return null;
  }
}

export function saveTasks(tasks) {
  try {
    localStorage.setItem(KEY, JSON.stringify(tasks));
  } catch {
    // Storage can be full or switched off. The app still works without it.
  }
}

localStorage holds strings, so tasks are saved as JSON text and parsed back. Both functions catch errors, because storage can be disabled or full. Loading also checks the data's shape: valid JSON such as {} is not a task array. Duplicate ids are rejected too, because React needs unique keys.

Then change three things in src/App.jsx:

JavaScript
import { useEffect, useState } from "react";
import { loadTasks, saveTasks } from "./storage.js";
JavaScript
const [tasks, setTasks] = useState(() => loadTasks() ?? starterTasks);
JavaScript
useEffect(() => {
  saveTasks(tasks);
}, [tasks]);

Add a task, then reload. It's still there.

Now check the other two cases before moving on. Clear every task and reload: the list stays empty, because an empty array is valid saved data. Then, in the browser console, run localStorage.setItem("study-tasks", "{}") and reload: {} isn't a task array, so the app falls back to the starter tasks.

Two details to understand

The function passed to useState. useState(() => loadTasks() ?? starterTasks) passes a function, not a value. React calls it only for the first render. useState(loadTasks() ?? starterTasks) would also work, but it would read storage on every render and throw the result away. ?? uses the starter tasks when loadTasks returns null, meaning nothing's saved yet.

The effect keeps something outside React in step. Storage is outside React, exactly what Day 6 said effects are for. With [tasks] as the dependency list, every change to the tasks is saved, whatever caused it.

StrictMode

npm create vite would give you a main.jsx that wraps <App /> in <StrictMode>. The downloaded labs don't. Add it if you like: it runs effects twice in development to catch missing cleanups. This effect is safe to run twice, because saving the same tasks twice changes nothing.

Keep this project. The next lesson adds tests to it.

Was this article helpful?

Your response is saved on this device.