#[SOLVED] Not deleting at first

35 messages · Page 1 of 1 (latest)

final sigil
#

Previously I made a todo-list and now I'm adding appwrite as backend, and right now I'm facing the issue, when I click the todo delete button it doesn't delete the todo but when I click the delete button again the its delete the todo.

dapper oracle
final sigil
#

wait I'm sahring the github repo

#

and also I'm sharing a video which helps to understand what is the problem

final sigil
dapper oracle
final sigil
#

yes

dapper oracle
# final sigil yes

One thing that might be the problem is to make some sort of state change after the API call. If there's no system change, there's no re-render

final sigil
#

and after that it reloads the page

dapper oracle
dapper oracle
final sigil
#

yes bcz after printing the statement page relaods itself

final sigil
#
const deleteTodoHandler = async (id) => {
    try {
      db.deleteDocument(
        import.meta.env.VITE_APPWRITE_DATABASE_ID,
        import.meta.env.VITE_APPWRITE_COLLECTION_ID,
        id
      );
      console.log('deleted');
      window.location.reload();
    } catch (err) {
      console.log(err);
      window.location.reload();
    }
  };
#

here is the code

dapper oracle
final sigil
dapper oracle
final sigil
#

I know but in the useEffect function if I gave the any payload or leave it empty it calls the function infinitely

#
useEffect(() => {
    const loadData = async () => {
      setLoader(true);
      try {
        const getTodos = await db.listDocuments(
          import.meta.env.VITE_APPWRITE_DATABASE_ID,
          import.meta.env.VITE_APPWRITE_COLLECTION_ID
        );
        setTodos(getTodos.documents);
      } catch (err) {
        console.log(err);
      }
    };
    loadData();
    setLoader(false);
  }, []);
dapper oracle
final sigil
#

see this video

#

if I gave any payload then its happening

dapper oracle
final sigil
#
useEffect(() => {
    const loadData = async () => {
      setLoader(true);
      try {
        const getTodos = await db.listDocuments(
          import.meta.env.VITE_APPWRITE_DATABASE_ID,
          import.meta.env.VITE_APPWRITE_COLLECTION_ID
        );
        setTodos(getTodos.documents);
      } catch (err) {
        console.log(err);
      }
    };
    loadData();
    setLoader(false);
  }, [todos]);
final sigil
dapper oracle
final sigil
#

Yes I know but I found a solution using useReducer

#

Its work on the submit function and load function correctly

#

Now I'm implementing it on the deleting function

final sigil
#

Thanks @dapper oracle to help me. Now it is working perfectly.