Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!