function App() {
const [input, setInput] = useState("");
const [tasks, setTasks] = useState([]);
function handleChange(event) {
const value = event.target.value;
setInput(value);
}
function handleClick() {
setTasks([...tasks, input]);
setInput("");
}
return (
<div className="container">
<div className="heading">
<h1>To-Do List</h1>
</div>
<div className="form">
<input type="text" value={input} onChange={handleChange} />
<button type="submit" onClick={handleClick}>
<span>Add</span>
</button>
</div>
<div>
<ul>
{tasks.map((task) => {
return <li>{task}</li>;
})}
</ul>
</div>
</div>
);
}
Comparing option 1:
function handleClick() {
setTasks([...tasks, input]);
setInput("");
}
with option 2:
function handleClick() {
setTasks((prevValue) => [...prevValue, input]);
setInput("");
}
What are the implications of using calling the current state of tasks
as opposed to option 2 in which prevValue
is used to manage the complex state of tasks
?
Both seem to work similarly in terms of functionality, but I understand from tutorial that option 2 preferable. Hence, I would like to understand what can happen if I don't use option 2?