I'm trying to create a todo app with the functionality to drag and drop items around. I'm using React DND and had everything working so far, but when a new item is added to the list, I realized that the function called within the "drop" method in useDrop would only use the state from when the page is first loaded up.
No other component has this issue, and when I tried a simple onClick to check the state on the same component, the correct state would be logged as well. The other components (such as a component that inserts a new task) are not having issues at all, this is only happening on this component that is using React DND. I've also checked the react dev tools in chrome and can confirm that the state is being updated as well.
The function that is being called within "drop" is passed down from two components up, and the state that I am trying to use is in that component as well.
Any help will be appreciated. Thank You.
export const ToDo = ({
status,
todoText,
todoID,
deleteTodo,
updateTodoStatus,
resortList,
}: TodoProps) => {
const [, drag] = useDrag(() => ({
type: 'TODO_DRAG',
item: { todoID, todoText },
}));
const [, drop] = useDrop(() => ({
accept: 'TODO_DRAG',
drop: (droppedTodo: dropAndDropItem) => {
console.log('dropping');
if (todoID === droppedTodo.todoID) return;
resortList(todoID, droppedTodo.todoID);
},
}));
return (
<li ref={drop} onClick={() => resortList(1, 2)}>
<div ref={drag}>
<label htmlFor="Change Todo Status">
<input
type="checkbox"
checked={status}
onChange={() => updateTodoStatus(todoID)}
/>
</label>
<p>{todoText}</p>
<button onClick={() => deleteTodo(todoID)}>X</button>
</div>
</li>
);
};