Why is useEffect()
looping endlessly when the second set of arguments is being passed in []
? The goal here is to get the state of the last record of props.state.hours.clockedIn
on-load only (just once), and then store that boolean value to be used elsewhere. If the second argument does not have props.hours
and clocked
then useEffect
does not get the value of the last record and is always false
(when it should be true
in this particular case). If they are passed in, I can see in the console.log the .map
function is working as intended. Does useEffect()
need to be used here? Is there another method to achieve getting just that last record's clockedIn
value on-load?
const Hours = (props) => {
const [clocked, setClocked] = useState(false);
useEffect(() => {
props.fetchHours()
.then(() => {
_.map(props.state.hours, (hour) => {
setClocked(hour.clockedIn)
console.log(clocked)
})
})
}, [props, clocked])
return (
<div></div>
);
};