My question is this, I'm having an array I need to monitor its change in my componentDidUpdate lifecycle, now I'm trying to rewrite the component in hooks, I'm having issue in doing so in hooks, a lot of the solutions I found on the internet suggests to create a customized usePrevious hook like this
const usePrevious = value => {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
};
But I found that this hook can only deal with primitive data type, it doesn't seem to work with object or array...
componentDidUpdate(prevProps) {
if (!_.isEqual(myArray, prevProps.myArray)) {
// do something...
}
}
From what you're saying I think using a useEffect hook that has the prop you want to watch for as dependency and the logic inside should do the trick, you can check the React Docs for a more detailed explanation, but basically, it would look like this
useEffect(() => {
// code to run when it changes
}, [yourVar]);