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

react-native - useQuery().filtered() returns new object every render #6014

Closed
aclelland-netfore opened this issue Jul 28, 2023 · 1 comment
Closed

Comments

@aclelland-netfore
Copy link

aclelland-netfore commented Jul 28, 2023

How frequently does the bug occur?

Always

Description

setting up a new App and just started learning how to use Realm and noticed this issue with useQuery and filtered() where filtered() will return a new object every render. I've managed to get around this by manually memorizing based on useQuery but I'm pretty sure this is a bug.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

const useItems = (parentId: number) => {
  const {useQuery} = useRealmContext(); // useQuery doesn't work globally at the moment, the Provider also seems broken.

  const items = useQuery(Item); // this is fine, doesn't change
  useEffect(() => {
    // only when parentId changes  as expected
  }, [items])

  const filteredItems = items.filtered("parentId = $0", parentId); // this is a new object
  useEffect(() => {
    // every render
  }, [filteredItems])

  return filteredItems 
}

Version

0.5.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

React-Native, Expo, Android

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@takameyer
Copy link
Contributor

takameyer commented Jul 29, 2023

@aclelland-netfore We actually just released a way around this in v0.5.0. You can apply filters and sorts in a callback:

  const tasks = useQuery(
    Task,
    collection =>
      showDone
        ? collection.sorted('createdAt')
        : collection.filtered('isComplete == false').sorted('createdAt'),
    [showDone],
  );

This will be memoized and only return new objects when they have changed.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants