Ligne 4 :
Ligne 4 :
* [https://learn.microsoft.com/en-us/windows/dev-environment/javascript/react-overview Microsoft doc]
* [https://learn.microsoft.com/en-us/windows/dev-environment/javascript/react-overview Microsoft doc]
* [https://react.dev/learn/react-developer-tools React Developer Tools]
* [https://react.dev/learn/react-developer-tools React Developer Tools]
* [Javascript#LINQ_équivalent|LINQ equivalent]
= Description =
= Description =
Version du 26 octobre 2024 à 16:46
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
npm create vite@latest
npm install
npm run dev
Basic App
public/index.html
<body >
<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 > ,
)
afficher package.json
{
"name" : "vite-project" ,
"private" : true ,
"version" : "0.0.0" ,
"type" : "module" ,
"scripts" : {
"dev" : "vite" ,
"build" : "tsc -b && vite build" ,
"lint" : "eslint ." ,
"preview" : "vite preview"
} ,
"dependencies" : {
"react" : "^18.3.1" ,
"react-dom" : "^18.3.1"
} ,
"devDependencies" : {
"@eslint/js" : "^9.13.0" ,
"@types/react" : "^18.3.11" ,
"@types/react-dom" : "^18.3.1" ,
"@vitejs/plugin-react" : "^4.3.3" ,
"eslint" : "^9.13.0" ,
"eslint-plugin-react-hooks" : "^5.0.0" ,
"eslint-plugin-react-refresh" : "^0.4.13" ,
"globals" : "^15.11.0" ,
"typescript" : "~5.6.2" ,
"typescript-eslint" : "^8.10.0" ,
"vite" : "^5.4.9"
}
}
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 >
Allows to store values.
const [items, setItems] = useState<Item []>([]);
Allows to connect a component to an external system.
useEffect (() => {
const fetchData = async ( ) => {
const result = await axios (`${API} ?query=react` );
};
fetchData ();
}, []);
export default function Button ( ) {
function handleClick ( ) {
alert ('You clicked me!' );
}
return (
<button onClick ={handleClick} >
Click me
</button >
);
}
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}"
}
]
}
import App from './App.tsx'
Component libraries