I am trying to create a function that when gets called, it sets the state to the object {item: 'whatever the user types', list: [...list, todo]}; However, I don't know how to set the new state and return <li>{todo}</li> at the same time. I have been stuck for a while so I'd appreciate if you can help me out.
const Todo = () => {
const [state, setState] = useState({ item: '', list: [] });
const setTodoValue = (e) => {
setState((previousState) => {
return { ...previousState, item: e.target.value };
});
};
const addTodo = () => {
setState((previousState) => {
return {
...previousState,
list: [...previousState.list, previousState.item],
};
});
};
return (
<div>
<input onChange={setTodoValue} type="text" />
<button onClick={addTodo}> + </button>
{}
</div>
);
};