Can’t we just use the prop value directly instead of wrapping it in a useEffect?
Say,
Component = ({bool}) => {
If (bool) playSound()
return … }
Versus:
…
UseEffect(() => if(bool) playSound(), [bool])
…
These two cases have same behavior.
Update: It’s better to use useEffect, which only fires the side effect when the dependency changes when a component re-renders. It guards against incorrectly triggering the side effect if the dependency did not change value upon component re-render.
reactjs - Change state conditioned on in useEffect without triggering the hook and without breaking the exhaustive dependencies rule - Stack Overflow
reactjs - Is it okay to change boolean which is a hook and dependency of useEffect in React? - Stack Overflow
reactjs - How do we check the boolean value in useEffect in react hooks? - Stack Overflow
reactjs - Better way to execute useEffect when boolean changes in one direction? - Stack Overflow
You can add another useEffect which watches this change, useEffect takes a second argument which is dependency array and the effect gets called if any of the dependency array value changes .
In this case since you need to make a decision based on the nomStatus, you can add it as a dependency to your useEffect
useEffect(() => {
if (nomStatus) {
setShowCalender(true);
}
}, [nomStatus]);
You can't since React state updates are asynchronously processed, the nomStatus state update won't be available until the next render cycle. Use the res.data[0].status value to set the showCalendar state.
const [nomStatus, setNomStatus] = useState(false);
useEffect(() => {
const fetchData = async () => {
const email = localStorage.getItem("loginEmail");
try {
const res = await Axios.get(
"http://localhost:8000/service/activeStatus",
{email}
);
setNomStatus(res.data[0].status);
console.log("Get status data :" + res.data[0].status);
if (res.data[0].status){
setShowCalender(true);
}
} catch (e) {
console.log(e);
}
};
fetchData();
}, []);
Or you can use a second useEffect hook with a dependency on nomStatus state update to set the showCalendar state.
useEffect(() => {
const fetchData = async () => {
const email = localStorage.getItem("loginEmail");
try {
const res = await Axios.get(
"http://localhost:8000/service/activeStatus",
{email}
);
setNomStatus(res.data[0].status);
console.log("Get status data :" + res.data[0].status);
} catch (e) {
console.log(e);
}
};
fetchData();
}, []);
useEffect(() => {
if (nomStatus){
setShowCalender(true);
}
}, [nomStatus]);
Okay I read about this somewhere... I wish I could give credit where it's due.
But you build a conditional outside your useEffect... for cleanliness... and then put that in the useEffect dep array.
See attached image.
I am finding my useEffect runs regardless of the conditional... which maybe I need to state in the dependency array... conditional === true...
Dang as I type this... that is probably my problem. But I'll lean it here incase anybody else has not see something like this before.
https://www.instagram.com/p/Cb04lovOFOV/
Sorry for the instagram drop.. but apparently the reddit react doesn't let images to be uploaded. And I'm too lazy to open VS code and copy the code again. Sorry!