Click here to Skip to main content
15,939,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
technologies used: React Native / Expo / Firebase

I explain to you, I recover a list of key with my first onValue, in this one I forEach for each key and in this one I make another onValue in order this time to recover the information of the keys, but I realize that it creates a problem, one event calls another and it creates duplicate calls. Moreover I could not intervene on the second listening to delete it since it is only initialized locally, so how can I call this in the right way? thank you in advance for your answers, I'm new to react native with firebase!

Here is a snippet of the code:

useFocusEffect( // first time the screen is loaded, the gardener list is empty, so we need to get the gardener list from firebase

  React.useCallback(() => {

      const gardenerRef = ref(db, 'users/' + auth.currentUser.uid + '/gardeners');
      const gardenerListener = onValue(gardenerRef, (snapshot) => {
          const data = snapshot.val();
          if (data != null) {
              Object.keys(data).forEach(e => {
                  const temp = ref(db, 'gardeners/' + e);
                  onValue(temp, (snapshot) => {
                      const data = snapshot.val();
                      if (data != null && data.metadata != undefined) {
                          setGardeners(gardeners => [...gardeners, { id: e, name:, isIrrig: data.irrig }]);
                  gardeners.length > 0 ? setCurrentGardener(gardeners[0].id) : setCurrentGardener("")
                  setC(currentGardener != "" ? currentGardener : "")
          } else {

and here is the way i turn off the event during disassembly, for me it looks correct, now if you think i did it wrong, don't hesitate.

return () => {
             off(gardenerRef, gardenerListener)
             // console.log("unmounted")
  }, []))

What I have tried:

I tried to take the second onValue out of the first, but without success I can't get access to my data, I also try to off it but I can't because the databaseReference and the listener are only initialized locally, and on React with javascript you cannot create a lateinit variable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900