Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
state inside realm listener is undefined (Realm and React Native)

I am facing the following problem with Realm and React Native.

Inside my component I want to listen to changes of the Realm, which does work. But inside the listener, my state is always undefined - also after setting the state inside the listener, the useEffect hook does not trigger. It looks like everything inside the listener doesn't have access to my state objects.

I need to access the states inside the listener in order to set the state correctly. How can I do this?

edit: The state seems to be always outdated. After hot reloading, the state is correct, but still lags behind 1 edit always.

  const [premium, setPremium] = useState<boolean>(true);
  const [settings, setSettings] = useState<any>({loggedIn: true, userName: 'XYZ'});

  useEffect(() => {
    console.log('updated settings'); // never gets called
  }, [settings]);

  useEffect(() => {
    const tmp: any = settingsRealm.objects(MyRealm.schema.name)[0];
    tmp.addListener(() => {
      console.log(premium, settings); // both return undefined
      if (premium) {
        setSettings(tmp);
      }

      // for demonstration purposes
      setSettings(tmp);
    })
  }, []);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For anyone having the same problem:

Adding a useRef referencing the state, and updating both(!) at the same time (state and ref) will solve the problem. Now all instances (inside the listener) of "premium" will have to be changed to "premiumRef.current".

https://medium.com/geographit/accessing-react-state-in-event-listeners-with-usestate-and-useref-hooks-8cceee73c559

  const [premium, _setPremium] = useState<boolean>(true);
  const [settings, setSettings] = useState<any>({loggedIn: true, userName: 'XYZ'});

  const premiumRef = useRef<boolean>(premium);
  const setPremium = (data) => {
    premiumRef.current = data;
    _setPremium(data);
  }


  useEffect(() => {
    const tmp: any = settingsRealm.objects(MyRealm.schema.name)[0];
    tmp.addListener(() => {
      if (premiumRef.current) {
        setSettings(tmp);
      }
    })
  }, []);


  [1]: https://medium.com/geographit/accessing-react-state-in-event-listeners-with-usestate-and-useref-hooks-8cceee73c559
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda