This code is supposed to add new tasks to the state and keep the previous objects, past posts on this have lead to the same issue that it just wont be added.
function App() {
var [tasks, setTasks] = useState([
{
id: uuidv4(),
text: 'Meeting at place',
specify: 'todo',
},
{
id: uuidv4(),
text: 'Shopping',
specify: 'inprogress',
},
{
id: uuidv4(),
text: 'Appointment',
specify: 'complete',
},
])
//Add Task
const newTask = (text) => {
const id = uuidv4();
var specify = 'todo'
const makeTask = { id, ...text, specify }
console.log(makeTask)
console.log([...tasks, makeTask])
setTasks(tasks => [...tasks, makeTask])
console.log(tasks)
}
when I log what's inside the set function it shows all 4 but it doesnt get saved into tasks
My drag and drop function which filters by id works with the predefined object literals but not new added tasks, and instead returns undefined with new tasks.
Edit: useEffect was solution to correctly show my state for those with same issue. My issue was more complex due to react dnd and found solution here React Hooks: Can't access updated useState variable outside useEffect? .