Shared State
- Learn to manage state in a parent component.
- Share state between sibling components by lifting the state to a common parent.
- Learn to handle an input form using the
controlled componentpattern.

Use Vite to create, and then remove irrelevant code from the source code.
App: The parent component that holds the shared state.AddTodo: A child component where users can input and add todos.TodoList: A child component that displays the list of todos.
import React, { useState } from 'react';
import AddTodo from './AddTodo';
import TodoList from './TodoList';
const App = () => {
const [todos, setTodos] = useState([]); // Shared state for todos
const addTodo = (todo) => {
setTodos([...todos, todo]); // Add a new todo to the list
};
return (
<div>
<h1>Todo List</h1>
<AddTodo onAddTodo={addTodo} /> {/* Pass the state updater to AddTodo */}
<TodoList todos={todos} /> {/* Pass the shared state to TodoList */}
</div>
);
};
export default App;
import React, { useState } from 'react';
const AddTodo = ({ onAddTodo }) => {
const [inputValue, setInputValue] = useState('');
const handleAdd = () => {
if (inputValue.trim() === '') return; // Prevent empty todos
onAddTodo(inputValue); // Call the function passed from the parent
setInputValue(''); // Clear the input
};
return (
<div>
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Enter a todo"
/>
<button onClick={handleAdd}>Add Todo</button>
</div>
);
};
export default AddTodo;
import React from 'react';
const TodoList = ({ todos }) => {
return (
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo}</li> // Display each todo
))}
</ul>
);
};
export default TodoList;
State Management:
- The
Appcomponent holds the sharedtodosstate. - This state is passed down to the
TodoListcomponent as props. - A state updater function (
addTodo) is passed to theAddTodocomponent as props.
- The
Props:
- The
Appcomponent uses props to communicate with its child components. AddTodouses theonAddTodoprop to send data to the parent.TodoListuses thetodosprop to receive data from the parent.
- The
State Lifting:
- The
todosstate is lifted to theAppcomponent so that it can be shared betweenAddTodoandTodoList.
- The
- Add another component,
TodoCount, that shows the total number of todos. Pass thetodosarray to it using props. - Modify the
TodoListto allow removing a todo when clicking on it. Teach how to update the state from the child component via a callback function. - Style the application with css (make it nice and responsive)
By completing this exercise, you will:
- Understand how to manage shared state in React.
- Learn to lift state to a common parent component.
- Become comfortable passing data between parent and child components using props.