Lançado · em melhoria
Guia de React · 1/6
Por enquanto, este capítulo está disponível apenas em inglês.
This chapter sets up the tools you need for React development, creates a first project with Vite and runs it on the dev server. Along the way it covers a sensible editor setup.
React itself is a library that runs in the browser, but the tools that create and build a project all run on Node.js. Install the LTS (long-term support) release from nodejs.org, then open a new terminal and check the versions.
node --version
npm --versionInstalling Node.js also installs the npm package manager. pnpm, Yarn or Bun work just as well, but this guide uses npm commands throughout.
The official React docs recommend starting new apps with a framework such as Next.js, React Router (framework mode) or Expo (for mobile), because frameworks bring routing, data loading, server rendering and code splitting out of the box. When you want to learn React's core ideas, or build a single-page app that needs no server of its own, starting with the Vite build tool keeps the setup small and easy to understand.
| Option | Good fit for |
|---|---|
| Vite | Learning, client-only SPAs, dashboards, frontends for an existing backend |
| Next.js | Server rendering, SEO-sensitive sites, full-stack apps |
| React Router (framework mode) | Web apps centered on routing and data loading |
| Expo | Mobile apps built with React Native |
This guide uses a Vite project so it can focus on React itself. Everything you learn about components, hooks and state carries over unchanged to any framework.
npm create vite downloads and runs Vite's project scaffolding tool. Passing --template react-ts creates a React template that uses TypeScript; choose the react template for plain JavaScript.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run devWith npm, the extra -- makes sure the template option reaches the scaffolding tool rather than npm itself. If you leave the template out, the tool asks you to pick a framework and variant interactively. Other package managers use these forms:
pnpm create vite my-app --template react-ts
yarn create vite my-app --template react-ts
bun create vite my-app --template react-tsnpm run dev starts the Vite dev server and prints an address such as http://localhost:5173. Keep that page open in your browser, edit src/App.tsx and save: the change shows up immediately without a full reload. Thanks to Fast Refresh, provided by Vite's React plugin, component state is usually preserved as well.
Try replacing the contents of src/App.tsx with this:
function Greeting({ name }: { name: string }) {
return <p>Hello, {name}!</p>;
}
export default function App() {
return (
<main>
<h1>My first React app</h1>
<Greeting name="React" />
<Greeting name="Vite" />
</main>
);
}Both Greeting and App are functions that return JSX, in other words components. Component names must start with a capital letter so React can tell them apart from HTML tags. To change the port, open the browser automatically or reach the server from a phone on the same network, pass options through:
npm run dev -- --port 3000 --open
npm run dev -- --hostVisual Studio Code supports TypeScript and JSX out of the box, so autocomplete, inline type errors and rename refactoring work immediately. A few additions make life easier:
eslint.config.js that ships with the Vite template and flags problems such as Rules of Hooks violations right in the editor.npm install -D prettier) keeps everyone on the same version.A project-level .vscode/settings.json can run formatting and ESLint auto-fixes on every save:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}During development you may notice components rendering twice or effects running twice. That is <StrictMode> in src/main.tsx doing extra work on purpose, only in development, to surface bugs such as missing cleanup early. It never happens in a production build.
npm create vite@latest my-app -- --template react-ts and run it with npm run dev.
0 comentários
Fazer login · Faça login para deixar um comentário.
Seja o primeiro a comentar.