Hey all given a basic react component that has componentWillReceiveProps
. I want to convert this to use react hooks. Now I've looked at this question here and whilst the answer is straightforward I feel as though I'm miss-understanding something. For example:
export class Example extends React.Component {
constructor(props) {
super(props);
}
componentWillReceiveProps(nextProps) {
console.log('HELLO WORLD') // Note this isn't called on initial render
if (nextProps.match.params.id != this.props.match.params.id) {
console.log('testing....')
}
}
render() {
return (
<div>Cool</div>
);
}
}
I know that from the above example that we won't get into the if statement unless the nextProps hasn't changed. Now converting the above to function component is like this:
export const Example = ({ ...props }) => {
useEffect(() => {
console.log('TESTING')
}, [props.match.params.id])
return (
<div>Cool</div>
)
}
I've also prepared a short recording gif of what I'm seeing. https://recordit.co/hPuwGey6WM