« React + Vite + TS » : différence entre les versions
Apparence
Ligne 113 : | Ligne 113 : | ||
.container { } | .container { } | ||
</filebox> | </filebox> | ||
= [https://react.dev/reference/react/hooks Hooks] = | |||
== [https://react.dev/reference/react/hooks#state-hooks State] == | |||
Allows to store values. | |||
<kode lang='tsx'> | |||
// items is the variable used to store the items | |||
// setItems is a function to set a new value to items | |||
const [items, setItems] = useState<Item[]>([]); | |||
</kode> | |||
= Visual Studio Code = | = Visual Studio Code = |
Version du 26 octobre 2024 à 15:30
Links
Description
React is a library. It lets you put components together, but it doesn’t prescribe how to do routing and data fetching.
To build an entire app with React, we recommend a full-stack React framework like Next.js or Remix.
Command line
# create a new project (framework: React, variant: TypeScript)
npm create vite@latest
# install the packages
npm install
# run the project
npm run dev
|
Basic App
public/index.html |
<body>
<!-- hosts the React application -->
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
|
src/main.tsx |
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<h1>My App</h1>
</StrictMode>,
)
|
package.json |
Component
src/App.tsx |
import { useState } from 'react'
import './App.css'
function App() {
const [count, setCount] = useState(0)
return (
<>
<h1>My App</h1>
</>
)
}
export default App
|
src/main.tsx |
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.tsx'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)
|
Styling
ClassName
App.tsx |
<div className="container"></div>
|
App.css |
.container { }
|
Hooks
State
Allows to store values.
// items is the variable used to store the items
// setItems is a function to set a new value to items
const [items, setItems] = useState<Item[]>([]);
|
Visual Studio Code
Debug
Install the Debugger for Firefox extension.
.vscode/launch.json |
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Firefox against localhost",
"type": "firefox",
"request": "launch",
"url": "http://localhost:5173",
"webRoot": "${workspaceFolder}"
}
]
}
|
Naming convention
- file name: PascalCase
// use the filename as the component name, so PascalCase
import App from './App.tsx'
|