An experienced software developer with a strong background in developing award-winning web and mobile applications for diverse clients with 6+ years of industry experience.
importReact,{useRef,useEffect,useState}from'react'// I also made it to support running when specific values update in deps// The default value for deps will be undefined if you did not pass it// and will have the same effect as not passing the parameter to useEffect// so it watch for general updates by defaultfunctionuseDidUpdate(callback,deps){consthasMount=useRef(false)useEffect(()=>{if(hasMount.current){callback()}else{hasMount.current=true}},deps)}// Usage:functionApp(){useDidUpdate(()=>{doStuff()})}// Or:functionAppTwo(){const[count,setCount]=useState(0)useDidUpdate(()=>{doStuffIfCountUpdates()},[count])}
Wow guys that really solved a problem i had.. useeffect doesn't imitate componentDidUpdate even after using prop as the last parameter , i used Hammeds solution with useDidUpdate and i just added a prevprops var . seems to be working fine so far.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
How about converting this to a custom hook?
✌️✌️✌️
Wow guys that really solved a problem i had.. useeffect doesn't imitate componentDidUpdate even after using prop as the last parameter , i used Hammeds solution with useDidUpdate and i just added a prevprops var . seems to be working fine so far.