Tutorial - React SPA with an API
import { PackageManagers, Render } from "~/components";
This tutorial takes you through the steps needed to adapt a Vite project to use the Cloudflare Vite plugin. Much of the content can also be applied to adapting existing Vite projects and to front-end frameworks other than React.
:::note If you want to start a new app with a template already set up with Vite, React and the Cloudflare Vite plugin, refer to the React framework guide. To create a standalone Worker, refer to Get started. :::
Introduction
In this tutorial, you will create a React SPA that can be deployed as a Worker with static assets. You will then add an API Worker that can be accessed from the front-end code. You will develop, build, and preview the application using Vite before finally deploying to Cloudflare.
Set up and configure the React SPA
Scaffold a Vite project
Start by creating a React TypeScript project with Vite.
Next, open the cloudflare-vite-tutorial directory in your editor of choice.
Add an API Worker
Call the API from the client
Edit src/App.tsx so that it includes an additional button that calls the API and sets some state:
import { useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";
function App() {
const [count, setCount] = useState(0);
const [name, setName] = useState("unknown");
return (
<>
<div>
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button
onClick={() => setCount((count) => count + 1)}
aria-label="increment"
>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<div className="card">
<button
onClick={() => {
fetch("/api/")
.then((res) => res.json() as Promise<{ name: string }>)
.then((data) => setName(data.name));
}}
aria-label="get name"
>
Name from API is: {name}
</button>
<p>
Edit <code>api/index.ts</code> to change the name
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
);
}
export default App;
Now, if you click the button, it will display 'Name from API is: Cloudflare'.
Increment the counter to update the application state in the browser.
Next, edit api/index.ts by changing the name it returns to 'Cloudflare Workers'.
If you click the button again, it will display the new name while preserving the previously set counter value.
With Vite and the Cloudflare plugin, you can iterate on the client and server parts of your app together, without losing UI state between edits.
Next steps
In this tutorial, we created an SPA that could be deployed as a Worker with static assets. We then added an API Worker that could be accessed from the front-end code. Finally, we deployed both the client and server-side parts of the application to Cloudflare.
Possible next steps include:
- Adding a binding to another Cloudflare service such as a KV namespace or D1 database
- Expanding the API to include additional routes
- Using a library, such as Hono or tRPC, in your API Worker
谢谢你的反馈!