React Js todo list Example
React Js Todo List Example: Reactjs TodoList example is a simple web application that demonstrates the basic concepts of Reactjs. It allows users to create, read, update, and delete tasks in a to-do list. The app utilizes React's component-based architecture, state management, and event handling to dynamically render the list and handle user interactions. By leveraging React's virtual DOM, the app efficiently updates the UI in response to user actions, providing a smooth user experience. The example is often used as a starting point for beginners to learn React js and understand its fundamental principles.




Thanks for your feedback!
Your contributions will help us to improve service.
How can I create a simple todo list app using ReactJS?
The given code is a simple Reactjs todo list example. It uses the React library and its hooks, such as useState, to manage the state of the todo list. The App component renders a list of todos with checkboxes to mark them as completed. Users can add, remove, and edit todos using the provided input field and buttons. The completed todos are displayed with a line-through text style and red color. The code demonstrates basic Reactjs concepts like state management, event handling, and conditional rendering for editing todos.
React Js Todo List Example
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
// Define the App component
const App = () => {
const [todos, setTodos] = React.useState([]);
const [newTodo, setNewTodo] = React.useState("");
const [editIndex, setEditIndex] = React.useState(-1); // Index of the todo being edited
// Function to handle input change
const handleInputChange = (event) => {
setNewTodo(event.target.value);
};
// Function to add a new Todo
const addTodo = () => {
if (newTodo.trim() !== "") {
if (editIndex !== -1) {
// If editIndex is not -1, we are in edit mode
const updatedTodos = [...todos];
updatedTodos[editIndex] = { text: newTodo, completed: false };
setTodos(updatedTodos);
setNewTodo("");
setEditIndex(-1); // Reset editIndex after updating the todo
} else {
setTodos([...todos, { text: newTodo, completed: false }]);
setNewTodo("");
}
}
};
// Function to remove a Todo
const removeTodo = (index) => {
setTodos(todos.filter((_, i) => i !== index));
};
// Function to toggle the completion status of a Todo
const toggleComplete = (index) => {
const updatedTodos = [...todos];
updatedTodos[index].completed = !updatedTodos[index].completed;
setTodos(updatedTodos);
};
// Function to edit a Todo
const editTodo = (index) => {
setNewTodo(todos[index].text);
setEditIndex(index);
};
// JSX for the Todo list
return (
<div className="todo-container">
<h3>Reactjs todo list Example</h3>
<input
type="text"
value={newTodo}
onChange={handleInputChange}
className="todo-input"
/>
<button onClick={addTodo} className="todo-button">
{editIndex !== -1 ? "Edit Todo" : "Add Todo"}
</button>
<ul className="todo-list">
{todos.map((todo, index) => (
<li key={index} className="todo-item">
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleComplete(index)}
className="todo-checkbox"
/>
<span
style={{
textDecoration: todo.completed ? "line-through" : "none",
color: todo.completed ? "red" : "black",
}}
className="todo-text"
>
{todo.text}
</span>{" "}
<button
onClick={() => removeTodo(index)}
className="todo-remove-button"
>
Remove
</button>{" "}
<button
onClick={() => editTodo(index)}
className="todo-edit-button"
>
Edit
</button>
</li>
))}
</ul>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("app"));
</script>