Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2702

Custom hook re-render multiple components (All App tree)

      How frequently does the bug occur?

      Always

      Description

      Hello,

      I would like to have some guidance on my custom hook use case.

      I have created a hook that manage my user :
      useUserManager.js looks like :

      export function useUserManager() {
        const realm = useRealm();
        const realmUser = useUser();
      
        const user = useObject("User", Realm.BSON.ObjectId(realmUser.id));
        const addUser = useCallback(() => {
          realm.write(() => {
            realm.create(
              User,
              {
                _id: Realm.BSON.ObjectId(realmUser.id),
                name: realmUser?.profile?.name,
                email: realmUser?.profile?.email.toLowerCase(),
              },
            );
          });
        }, [realmUser.id, realmUser?.profile?.email]);
        const addUserCoordinate = useCallback((myCoords) => {.......}
        const removeUserCoordinate = useCallback(() => {.......}
      return {user, addUser, addUserCoordinate, removeUserCoordinate}
      

      I have a tree of component such as :
      > App.js > Parent.js > Child.js

      In my Child.js i am creating the user with addUser() and updating the coordinate with addUserCoordinate (fake example):

      {user, addUser, addUserCoordinate} = useUserManager()
      if (_.isEmpty(user){
         addUser()
      }
      addUserCoordinate(myCoords)
      

      Now that my user as coordinate in the database i would like to remove those coordinates when the application get closed, So i want to implement the removeCoordinate() function in the return of the useEffect at the highest level of my app (in App.js)

      App.js
      Hello,

      I would like to have some guidance on my hook use case.

      I have created a hook that manage my user :
      useUserManager.js looks like :

      export function useUserManager() {
        const realm = useRealm();
        const realmUser = useUser();
      
        const user = useObject("User", Realm.BSON.ObjectId(realmUser.id));
        const addUser = useCallback(() => {
          realm.write(() => {
            realm.create(
              User,
              {
                _id: Realm.BSON.ObjectId(realmUser.id),
                name: realmUser?.profile?.name,
                email: realmUser?.profile?.email.toLowerCase(),
              },
            );
          });
        }, [realmUser.id, realmUser?.profile?.email]);
        const addUserCoordinate = useCallback((myCoords) => {.......}
        const removeUserCoordinate = useCallback(() => {.......}
      return {user, addUser, addUserCoordinate, removeUserCoordinate}
      

      I have a tree of component such as :
      > App.js > Parent.js > Child.js

      In my Child.js i am creating the user with addUser() and updating the coordinate with addUserCoordinate (fake example):

      {user, addUser, addUserCoordinate} = useUserManager()
      if (_.isEmpty(user){
         addUser()
      }
      addUserCoordinate(myCoords)
      

      Now that my user has coordinate in the database i would like to remove those coordinates when the application get closed, So i want to implement the removeCoordinate() function in the return of the useEffect at the highest level of my app (in App.js)

      App.js

      const { removeUserCoordinate } = useUserManager();
      
        useEffect(() => {
          return () => {
              removeUserCoordinate();
          };
        }, []);
      

      But because i am calling the useUserManager hook in App.js, everytime i update the user object in my Child.js component all my app is re-rendering while i would like to have only the Child.js to re-render.

      How can I use my useUserManager hook to only re-render Child.js but being able to use it also in App.js to remove information at shutdown?

      Should I use something else than a hook?
      Should I create Functions instead of hooks?

      Thank you for you help

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Always

      Reproduction Steps

      No response

      Version

      "@gorhom/bottom-sheet": "^4.6.0", "@realm/react": "^0.6.2", "expo": "^49.0.8", "expo-dev-client": "~2.4.8", "expo-splash-screen": "~0.20.5", "expo-status-bar": "~1.6.0", "react": "18.2.0", "react-native": "0.72.6", "react-native-gesture-handler": "^2.14.1", "react-native-get-random-values": "~1.9.0", "react-native-paper": "^5.12.1", "react-native-reanimated": "^3.6.1", "react-native-safe-area-context": "^4.8.2", "realm": "^12.5.1"

      What services are you using?

      Atlas Device Sync

      Are you using encryption?

      No

      Platform OS and version(s)

      android

      Build environment

      Which debugger for React Native: ..

      Cocoapods version

      No response

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: