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

139
Vistas
why useEffect not updating automaticlly my state variable (maintenance)?

im trying to make a modal for maintenance system using remote config. the data is updated but when i want to update maintenance. the system always read status maintenance is false,and when i save the file then update the maintenance variable.

const [maintenance, setMaintenance] = useState(false);
const getMFAToken = async () => {
    try {
      const mfaToken = await AsyncStorage.getItem('mfaToken');
      // destructuring dari remote config
      const {STATUS, TEXT, TITLE} =  store.configReducer.configApp.MAINTENANCE;
      // set Maintenance untuk conditional rendering
      setMaintenance(STATUS);
      console.log(maintenance);
      

      // jika status true maka tidak lanjut ke login
      if (STATUS) {
        setMaintenance(STATUS);
        setTitle(TITLE);
        setMessage(TEXT);
      } else {
        if (mfaToken) {
          let response = await mfaValidation({mfaToken: mfaToken});
          console.log('ini mfa validation', response);
          if (!response.error && response.data.success) {
            let result = await mfaChallenge({
              mfaToken: mfaToken,
              challengeType: 'PIN',
            });
            console.log('ini mfa challenge', result);
            if (!result.error && result.data.success) {
              navigation.replace('PINScreen', {
                pinLength: result.data.inputLength,
                fromPage: 'Login',
              });
            } else {
              goToLogin();
            }
          } else {
            goToLogin();
          }
        } else {
          goToLogin();
        }
      }    
    } catch (error) {
      SendError.other({
        url: 'getMFAToken',
        file: 'SplashScrenn',
        message: error,
      });
    }
  };
useEffect(() => {
    ActivateConfig(err => {
      if (err) {
        Toast.show({
          type: 'error',
          text2: 'Silahkan periksa koneksi internet Anda dan coba kembali.',
        });
      } else {
        GetAllConfig(dispatch, () => {
          getMFAToken();
        });
      }
    });
    console.log("useEffect",maintenance);
  }, [maintenance]);

the maintenance can't update automaticly,i must save the file and will be changed the variable

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Where you're logging to the console in getMFAToken, maintenance will not be changed yet. React states do not update immediately.

If you change the console.log in getMFAToken to be

console.log({ maintenance, STATUS });

you can see if the two are the same or different. If they're the same, your useEffect won't run, as it will only run when the variables in the dependency array change. Setting state has a similar structure, in that it won't 'set' the state if the new state is the same as the old.

You're also calling setMaintenance twice in getMFAToken. This isn't causing an issue but I thought I would mention it.

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