Consider the above diagrammatic representation of a DOM tree where A, B, and C are components making up a part of the tree.
// src/A.js
function A(){
const [todo, setTodo] = useState([]);
return(
<>
<B setTodo={setTodo}/>
<C todos={todo}/>
</>
);
}
// src/B.js
function B({setTodo}){
const handleSubmit = useCallback((e)=>{
e.preventDefault();
setTodo(prevTodo => [...prevTodo, e.target.elements.todo.value ]
}, []);
return(
<form onSubmit={handleSubmit}>
<input type="text" id="todo" />
<button type="submit">Add Todo</button>
</form>
);
}
// src/C.js
function C({todo}){
return(
<ul>
{todos.map((todo)=>(
<li key={todo}>
{todo}
</li>
)}
</ul>
);
}