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

Component does not always re-render when array changes (useObject) #5185

Closed
mvrahas opened this issue Dec 17, 2022 · 6 comments · Fixed by #5208
Closed

Component does not always re-render when array changes (useObject) #5185

mvrahas opened this issue Dec 17, 2022 · 6 comments · Fixed by #5208
Assignees

Comments

@mvrahas
Copy link

mvrahas commented Dec 17, 2022

How frequently does the bug occur?

Seen always

Description

My task app schema includes an array of subtasks. When I push a new subtask to the array, the component doesn't re-render every time. It only re-renders every other time.

const TaskDisplay = ({_id})=>{

    const realm = useRealm()
    const task = useObject(Task, Realm.BSON.ObjectID(_id))

    const handleAddSubtask = (description) => {
        realm.write(() => {
          task.subtasks.push({description})
        })
    }

    console.log('RE-RENDER')

    return(
        <>
            <Button title={'Add Subtask'} onPress={()=>handleAddSubtask('This is just a subtask')}/>
        </>
    )
}

The interesting thing is that if I add in this Text below the Button, the component does re-render every time.

```{task ? task.subtasks.map((subtask,i)=>{subtask.description}) : null`}``

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

I created a sample app to demonstrate where this bug is happening for me..

  1. Download and run the app
  2. Click on 'Add task'
  3. Click on 'Add subtask'
    Expected result: 'RE-RENDER' is printed to the console
    Actual result: 'RE-RENDER' is not printed to the console

https://github.com/mvrahas/task-app-test

Version

0.4.1

What SDK flavour are you using?

Atlas Device Sync

Are you using encryption?

Yes, using encryption

Platform OS and version(s)

iOS 16.1.2

Build environment

Expo 47.0.3

Cocoapods version

No response

@kneth
Copy link
Member

kneth commented Dec 19, 2022

A quick look at your app, and I wonder if you need to add a subscription to Subtask. Flexible sync will not follow links, see the box about Object Link" in the documentation.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Dec 19, 2022
@kneth
Copy link
Member

kneth commented Dec 19, 2022

@mvrahas On the other hand, this issue is similar to the behaviour observed in #5182. Can you reproduce it you only use a local database?

@takameyer
Copy link
Contributor

@mvrahas Very strange that it happens every other time. I will investigate that part further. I would actually expect it to not re-render at all, and here is why.
useObject was designed to only re-render an objects list properties when they are visible and have changed. This was to prevent multiple re-renders, as the lists themselves contain logic to handle if they re-render or not. If the list items have never been accessed by the component rendering them, then the event listener will skip re-rendering.
Now, if you want to do some special event when adding subtasks, I would recommend implementing your own event listener on this collection.
Let me know if this helps!

takameyer added a commit that referenced this issue Dec 30, 2022
* Prime any list properties with an cachedCollection so that updates fire correctly (Fixes #5185)
* Primary Keys as non-primative values would reset the cached objects, since their reference always changes
* Create a listener on the collection if the object doesn't exist, and rerender when it is created (Fixes #4514)
@takameyer takameyer mentioned this issue Dec 30, 2022
11 tasks
takameyer added a commit that referenced this issue Dec 30, 2022
* Prime any list properties with an cachedCollection so that updates fire correctly (Fixes #5185)
* Primary Keys as non-primative values would reset the cached objects, since their reference always changes
* Create a listener on the collection if the object doesn't exist, and rerender when it is created (Fixes #4514)
@mvrahas
Copy link
Author

mvrahas commented Dec 30, 2022

Thank you @takameyer It makes sense to me that that would be the expected behavior. If you wouldn't mind linking any helpful documentation to create an custom event listener, that would help me out

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Dec 30, 2022
@takameyer
Copy link
Contributor

@mvrahas I have opened a PR #5208. There was actually a few issues with useObject which have been addressed. There should be indeed a re-render when anything changes in a useObject result. This was not happening for lists until they were actually accessed. Now the listeners are primed right away, which corrects this issue. I have tested this against the project you created, and now the RE-RENDER happens every time.

Thanks again for creating this issue and reproduction!

@mvrahas
Copy link
Author

mvrahas commented Dec 31, 2022

Awesome! Thank you

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Jan 3, 2023
takameyer added a commit that referenced this issue Jan 11, 2023
* Refactor useObject

* Prime any list properties with an cachedCollection so that updates fire correctly (Fixes #5185)
* Primary Keys as non-primative values would reset the cached objects, since their reference always changes
* Create a listener on the collection if the object doesn't exist, and rerender when it is created (Fixes #4514)
* Ensure listeners are added to one instance of a collection.

Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com>
Co-authored-by: Kræn Hansen <kraen.hansen@mongodb.com>
@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

Successfully merging a pull request may close this issue.

3 participants