Example output
To create a demo project with a todo list using React.js, you can follow these steps:
1. Project setup:
First, you need to set up a new React project using a tool like `create-react-app`:
```bash
npx create-react-app todo-list-demo
cd todo-list-demo
```
This will create a new React project with a default structure and install all necessary dependencies. You can then start the development server by running `npm start`.
2. Creating the Todo List component:
Inside the `src` folder, create a new folder called `components` and inside it, create a new file called `TodoList.js`. This file will contain the main Todo List component.
```javascript
import React, { useState } from 'react';
const TodoList = () => {
const [todos, setTodos] = useState([]);
const [input, setInput] = useState('');
const addTodo = (event) => {
event.preventDefault();
setTodos([...todos, input]);
setInput('');
};
return (
<div>
<h1>Todo List</h1>
<form onSubmit={addTodo}>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Add a todo..."
/>
<button type="submit">Add</button>
</form>
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo}</li>
))}
</ul>
</div>
);
};
export default TodoList;
```
In this component, we use the `useState` hook to manage the state of our todo list (`todos`) and the input field (`input`). We also define an `addTodo` function that adds a new todo item to the list when the form is submitted.
3. Including the Todo List component in the main App component:
Now, open the `src/App.js` file and update it to include the `TodoList` component:
```javascript
import React from 'react';
import TodoList from './components/TodoList';
function App() {
return (
<div className="App">
<header className="App-header">
<TodoList />
</header>
</div>
);
}
export default App;
```
4. Styling the Todo List component:
To style the Todo List component, you can add a new file called `TodoList.css` inside the `src/components` folder and include the styles you want. Then, import the CSS file in the `TodoList.js` file by adding `import './TodoList.css';` at the top of the file.
5. Running the demo project:
Now that your demo project is set up, you can run it by executing `npm start` in the terminal. This will start the development server, and you can view your todo list application in your web browser at `http://localhost:3000`.
That's it! You now have a simple demo project that uses React.js to create a todo list application. You can further enhance this project by adding features like deleting todos, editing todos, and marking todos as complete.