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
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.