Skip to content

Commit

Permalink
Fix rerendering issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j committed Jun 8, 2023
1 parent a54f291 commit 5a23eb2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions example-wasm/src/hooks/useTaskManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useEffect, useState } from 'react';

import { Task } from '../models/Task';

Expand All @@ -10,7 +10,14 @@ const { useQuery, useRealm, useUser } = await import('@realm/react');
export function useTaskManager() {
const realm = useRealm();
const user = useUser();
const tasks = useQuery(Task); // TODO: Fix rerendering
const [requeryFlag, setRequeryFlag] = useState(false); // Temporary flag
const tasks = useQuery(Task, (collection) => collection, [requeryFlag]);

useEffect(() => {
// Temporary solution for making `useQuery` update the `tasks` reference.
// (The value doesn't matter, only that it is different from the initial value.)
setRequeryFlag(true);
}, []);

const addTask = useCallback((description: string) => {
console.log('Adding task:', description);
Expand All @@ -37,6 +44,6 @@ export function useTaskManager() {
tasks,
addTask,
toggleTaskStatus,
deleteTask
deleteTask,
};
}

0 comments on commit 5a23eb2

Please sign in to comment.