0

How can we identify props changes in functional component?. Sample code here. Can you please help me to convert this componentDidUpdate method to functional componet using useEffect or any other way.

componentDidUpdate(prevProps){
   if(prevProps.props1 !== this.props.props1){
     //1st Function call
   }
   if(prevProps.props2 !== this.props.props2){
     //2nd Function call
   }
}
//-----------------

useEffect(()=>{

},[props1, props2])
Lineesh Mb
  • 53
  • 1
  • 9
  • Does this answer your question? [How to compare oldValues and newValues on React Hooks useEffect?](https://stackoverflow.com/questions/53446020/how-to-compare-oldvalues-and-newvalues-on-react-hooks-useeffect) – Lakshya Thakur May 07 '21 at 14:16

4 Answers4

2

Simplest solution is to use multiple useEffect invocations, one for each prop:

useEffect(()=>{
    // Something when props1 changes
},[props1])

useEffect(()=>{
    // Something when props2 changes
},[props2])

If you really need a combined useEffect (eg. if there is some shared logic), you can use useEffect in conjunction with usePrevious.

const prevProps1 = usePrevious(props1);
const prevProps2 = usePrevious(props2);

useEffect(() => {
    // compare props1 with prevProps1 and props2 with prevProps2
}, [props1, props2]);

Note that usePrevious is a custom hook from react-use that needs to be installed prior use (more info here: https://github.com/streamich/react-use. TL;Dr: use npm i react-use to install the package to use this (and other) hooks).

Mahsan Nourani
  • 294
  • 2
  • 17
lorefnon
  • 12,875
  • 6
  • 61
  • 93
  • although this approach is very fascinating, i wonder if the render can join multiple update request. Ex. if prevProps1 changed, and props1 changed as well, so will that trigger render twice in two different update? This question might not relate to `usePrevious`, i apologize. – windmaomao May 07 '21 at 14:26
  • ok, i guess the answer is no, maybe it's answered here already, https://windmaomao.medium.com/hook-myth-from-react-9495aa8ad7af, `setProp1` is called and then both `effects` will be updated **after** the rendering. – windmaomao May 07 '21 at 14:29
  • usePrevious will not trigger a re-render. And effect is triggered only when props1 changes and not prevProps1 changes. So you don't need to worry about double re-renders. – lorefnon May 07 '21 at 15:16
0

If you want to call different functions when different props change than you can multiple useEffect for each prop change.

Try something like below:-

useEffect(()=>{
   // function call when props1 change
},[props1])

useEffect(()=>{
   // function call when props2 change
},[props2])

Priyank Kachhela
  • 2,517
  • 8
  • 16
0

You can do 2 separate useEffect for that. You don't have to put it on a single useEffect since it's hard to identify which state did an update.

useEffect(() => {
  
}, [props1]);

useEffect(() => {
  
}, [props2]);
Techuila
  • 1,237
  • 8
  • 12
0

A modified approach to lorefnon's answer:

let {current: {p1, p2} = {}} = useRef()
useRef(() => {
  p1 = props1 // check with if (p1!=props1)
  p2 = props2 // check with if (p2!=props2)
}, [p1,p2]) // run effect when p1 or p2 change 
// basically states when props1 or props2 changes.
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231