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

useObject: causing re-render continously #6861

Open
yashsway opened this issue Aug 23, 2024 · 5 comments
Open

useObject: causing re-render continously #6861

yashsway opened this issue Aug 23, 2024 · 5 comments

Comments

@yashsway
Copy link

yashsway commented Aug 23, 2024

How frequently does the bug occur?

Always

Description

The underlying schema I'm referencing in useObject and the primary key are not changing at all which I've verified but the hook still returns brand new objects from the DB every few seconds.

I've had to solve this by manually writing the query using this alternative instead:

const data = useMemo(() => realm.objectForPrimaryKey(MySchema, variableWithId), [variableWithId]);

According to the docs the hook is supposed to only update on changes to the properties on the returned object?

The hook will update on any changes to the properties on the returned object and return null if it either doesn't exists or has been deleted.

I've verified that isn't the case. The properties are being read from only - not modified.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes

Reproduction Steps

  1. Have a valid Realm DB with a large Realm schema with some data
  2. Use useObject() in a React component
  3. Observe component re-render continously

Version

0.6.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

realm 12.3

Build environment

react-native: 0.73.6
expo: 50.0.0
react: 18.2.0
react-dom: 18.2.0

Cocoapods version

No response

Copy link

sync-by-unito bot commented Aug 23, 2024

➤ PM Bot commented:

Jira ticket: RJS-2895

@kraenhansen
Copy link
Member

kraenhansen commented Aug 24, 2024

What are you passing as arguments to your call of useObject? Please provide a full example.

@yashsway
Copy link
Author

What are you passing as arguments to your call of useObject? Please provide a full example.

@kraenhansen

This is in a react native app using Expo. Here's the component in which I was using the hook. This problem occurs everywhere where I use this hook but I'll just provide a snippet of one component...

const CreateFormSubmission = ({
  route: {
    params: {
      taskId,
    },
  },
  navigation,
}: FormStackScreenProps<"CreateFormSubmission">) => {
  // excluding other state here for brevity
  const cachedTask = useObject(Task, taskId ?? ""); // taskId doesn't change here - it's just a route param
  
  // ... excluded for brevity
  return <>some react native code</>;
};

where Task is a Realm schema that looks like:

export class Task extends Object<Task> {
  id!: string;
  cache_id!: string;
  cached_at?: Date;
  company_id!: string;
  facility_id!: string;
  assignee_user?: string;
  assignee_group?: string;
  name!: string;
  description!: string;
  status!: "complete" | "open" | "dismissed";
  type!: "manual_task" | "submit_form" | "complete_event";
  workflow!: WorkflowEmbedded;
  workflow_id?: string;
  form_submission?: FormSubmissionEmbedded[];
  config_form_schema?: FormSchemaEmbedded;
  emission_event?: ConfigEmissionEvent[];
  step_id!: string;
  created_at!: Date;

  static schema: ObjectSchema = {
    name: "Task",
    properties: {
      id: "string",
      cache_id: "string",
      cached_at: {
        type: "date",
        default: () => new Date(),
      },
      name: "string",
      assignee_user: "string?",
      assignee_group: "string?",
      description: "string",
      status: "string",
      type: "string",
      workflow: "WorkflowEmbedded",
      workflow_id: "string?",
      form_submission: "FormSubmissionEmbedded[]",
      config_form_schema: "FormSchemaEmbedded?",
      emission_event: "ConfigEmissionEvent[]",
      step_id: "string",
      created_at: "date",
    },
    primaryKey: "id",
  };
}

@kraenhansen
Copy link
Member

You're writing that you're using version "0.6.1" is that of @realm/react? I wonder if you've accidentally upgraded to use 0.8.0 or above and might be hitting #6842 which I just released a fix for in @realm/[email protected].

@yashsway
Copy link
Author

@kraenhansen

yes @realm/react ! specific version set is ^0.6.1 (actual installed version then seems to be 0.6.2). sorry that wasn't clear!

ah interesting...thanks for linking me to that. We get this issue quite often too (we have error logging through Sentry) so perhaps upgrading should fix the issue.

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

No branches or pull requests

3 participants
@kraenhansen @yashsway and others