I want to call a post API while changing the checkbox input. Right now I am doing this in the below pattern but while the Component re-renders, this API function got called. What to do, please?
const [check, setCheck] = useState();
async function fetchConfigApi() {
try {
const result = await axios.get(`/configuration/${system_id}`);
const { status, data } = result;
setConfigData(data);
setCheck(data.self_service);
} catch (e) {
console.log("error response", e.response);
}
}
async function postSelfService() {
try {
const fd = new FormData();
fd.append("self_service", check);
const result = await axios.put(`/configuration/${system_id}`, fd);
fetchConfigApi();
} catch (e) {
console.log("error response", e.response);
}
}
const onSelfServiceChange = (e) => { setCheck(!check); };
useEffect(() => { fetchConfigApi(); }, []);
useEffect(() => { postSelfService(); }, [check]);
return (
<div className={classes.subContainer}>
<div>
<FormControlLabel
control={
<Checkbox
checked={check}
onChange={onSelfServiceChange}
name="check"
/>
}
label="Enable Self Service"
/> </div> )
