Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda