Whenever i tried to put something in my todoList there’s alwasy an empty element at first indext Why it is happening?
const [todoList, setTodoList] = useState([]);
const addToList = (inputText) => {
if (inputText === "") {
alert("List is Empty")
}else{
setTodoList([inputText, ...todoList])
}
console.log(todoList);
};
const addList = (inputText) => {
addToList(inputText);
};
const [todoList, setTodoList] = useState([]);
const addToList = (inputText) => {
if (inputText === "") {
alert("List is Empty")
}else{
setTodoList([...todoList, inputText])
}
console.log(todoList);
};
const addList = (inputText) => {
addToList(inputText);
};
i tried it also but it’s not working
2
Answers
Your
is taking the
todoList
using closure, so it is taking the same one every time.You need to do something like:
I think adding useEffect() might do the trick: