I'm using React 17 and I wonder why the following components does not behave equally. When using react component class, props inside methods are updated whereas with Functional component, they are not.
Using the React.Component class (visible props is updated inside check method)
class Clock extends React.Component {
constructor(props) {
super(props);
}
check() {
console.log(this.props.visible);
}
componentDidMount() {
this.timerID = setInterval(
() => this.check(),
5000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
render() {
return (
<div />
);
}
}
Using function with hooks (visible props is NOT updated inside check method)
function Comp(props) { // contains visible attr (false by default)
const check = () => {
console.log(props.visible); // stays as the default value when Comp mounted
};
useEffect(() => {
const timerId = setInterval(() => {
check();
}, 5000);
return () => clearInterval(timerId);
}, []);
return <div />;
}
Does anybody has an idea?