npm install -g yarn
npx create-react-app todos-app
cd todos-app
index.js
et le fichier de départ de notre application. Nous allons supprimer le contenu du fichier src/index.css
et inclure bootstrap5 à la place
Nous allons s'abord installer bootstrap5 dans notre projet
yarn add bootstrap@next
Suite à l'installation, nous allons trouver le dossier bootstrap
dans node_modules
. Nous allons importer le fichier node_modules/bootstrap/dist/css/bootstrap.css
.
// src/index.js
import React from "react"
import ReactDOM from "react-dom"
import "bootstrap/dist/css/bootstrap.css"
import App from "./App"
// ... rien ne change ensuite
Le fichier HTML qui contient l'élément <div id="root"></div>
de notre application se trouve dans le répértoire public
.
C'est dans le fichier public/index.html
où nous devons modifier l'attribut lang, title et des attributs meta.
Nous n'avons pas besoin d'un fichier .css global. Nous allons supprimer le fichier App.css
et nous ne l'importons pas.
À la fin de cette étape notre app devrait afficher le titre ToDosApp
/* src/App.js */
import React from "react"
function App() {
return (
<div className="container my-4">
<h1 className="text-center">ToDos App</h1>
</div>
)
}
export default App
Nous avons besoin des components à inclure : <Todos />
, <Todo />
et <AddToDoForm />
Nous allons placer tous les components dans un nouveau dossier components
.
mkdir src/components
Voici la structure que nous allons créer :
src
├── App.js
├── App.test.js
├── components
│ ├── AddTodoForm.js
│ ├── Todo.js
│ └── Todos.js
├── index.css
├── index.js
├── serviceWorker.js
└── setupTests.js
Todos
utilise :
Todo
AddTodoForm
ainsi que la bibliothèque externe uuid, que nous devons installer
yarn add uuidv4
// src/components/Todos.js
import React, { useState } from "react"
import Todo from "./Todo"
import AddTodoForm from "./AddTodoForm"
import { uuid } from "uuidv4"
const Todos = () => {
return null
}
export default Todos
// src/components/Todo.js
import React from "react"
const Todo = () => {
return null
}
export default Todo
// src/components/AddTodoForm.js
import React from "react"
const AddTodoForm = () => {
return null
}
export default AddTodoForm
et notre App.js
// src/App.js
import React from "react"
import Todos from "./components/Todos"
function App() {
return (
<div className="container my-4">
<h1 className="text-center">ToDos App</h1>
<Todos />
</div>
)
}
export default App
- Avec
import React, { useState } from "react"
nous importonsuseState
directement donc nous n'avons plus besoin d'utiliserReact.useState
. uuid
est un named import, et nous allons utiliseruuid
au lieu deuuidv4
// src/components/Todos.js
import React, { useState } from "react"
import Todo from "./Todo"
import AddTodoForm from "./AddTodoForm"
import { uuid } from "uuidv4"
const Todos = () => {
const [todos, setTodos] = useState([])
const addTodo = (todo) => {
setTodos([...todos, { text: todo, isCompleted: false, id: uuid() }])
}
const toggleCompleteTodo = (todo) => {
setTodos(
todos.map((item) => {
if (item.id === todo.id) {
item.isCompleted = !item.isCompleted
}
return item
})
)
}
const deleteTodo = (todo) => {
setTodos(
todos.filter((item) => {
return item.id !== todo.id
})
)
}
const completedCount = todos.filter((el) => el.isCompleted).length
return (
<>
<h2 className="text-center">
Ma liste de tâches ({completedCount} / {todos.length})
</h2>
{todos.map((todo) => {
return (
<Todo
key={todo.id}
todo={todo}
handleCompleteClick={toggleCompleteTodo}
handleDeleteClick={deleteTodo}
/>
)
})}
<AddTodoForm handler={addTodo} />
</>
)
}
export default Todos
This project was bootstrapped with Create React App.
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
Builds the app for production to the build
folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed!
See the section about deployment for more information.
Note: this is a one-way operation. Once you eject
, you can’t go back!
If you aren’t satisfied with the build tool and configuration choices, you can eject
at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject
will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use eject
. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
You can learn more in the Create React App documentation.
To learn React, check out the React documentation.
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify