Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection reference from useQuery() not updating w/o dep. list #5929

Open
elle-j opened this issue Jun 22, 2023 · 2 comments
Open

Collection reference from useQuery() not updating w/o dep. list #5929

elle-j opened this issue Jun 22, 2023 · 2 comments

Comments

@elle-j
Copy link
Contributor

elle-j commented Jun 22, 2023

This is regarding React (not React Native) and our WASM build.

Dependencies Used

Problem

The following code works as expected in React Native but not in React:

const tasks = useQuery(Task);

Behavior:

  • When toggling or deleting a task, the tasks reference should be updated (it should be a new reference), but the reference does not get updated. Thus, the React component does not rerender.
  • @realm/react expectedly executes these lines:
    updatedRef.current = true;
    updateCallback();
  • @realm/react unexpectedly does not enter this if-block:
    if (updatedRef.current) {
    updatedRef.current = false;
    collectionRef.current = new Proxy(collection as Realm.Results<T & Realm.Object>, {});
    }

Workaround

The following code is a temporary solution users can implement as a workaround:

const [requeryFlag, setRequeryFlag] = useState(false);
const tasks = useQuery(Task, (collection) => collection, [requeryFlag]);
useEffect(() => {
  // The value doesn't matter, only that it is different from the initial value.
  setRequeryFlag(true);
}, []); // <-- Only on mount is needed

Note that the useEffect() only needs to run on mount for useQuery() to correctly update.

Reproduce

Minimal local Realm app to reproduce the behavior: react-userquery-bug.zip

Notes

This issue does not seem to appear in React Native.

@CatoPaus
Copy link

CatoPaus commented Feb 1, 2024

This is regarding React (not React Native) and our WASM build.

Dependencies Used

Problem

The following code doesn't work in React:
const task= useObject(Task, new BSON.ObjectId(user.id));

@elle-j FYI: I see the same issue on useObject() But if I place useObject() in the same hook together withuseQuery(), and use the workaround, it works as expected.

@elle-j
Copy link
Contributor Author

elle-j commented Feb 12, 2024

@CatoPaus, thanks for the additional information! We'll make sure to fix both use cases once we get to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants