0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

How to mimic componentDidUpdate using React Hooks? useDidUpdate Custom Hook | React Interview Question | JavaScript

@Devtools Tech
453

useEffect is a very useful hook. It works similarly to the componentDidMount() and componentDidUpdate() methods for class components.

However, sometimes, we want to execute the hook only when the component updates.

In this question, you need to solve this particular problem only. You need to implement a custom hook called useDidUpdate that takes a callback and dependency array as parameters. It only invokes the callback whenever the dependency array changes i.e. component updates.

Syntax

useDidUpdate(callback, deps);

Arguments

  • callback: Function: The function that needs to be invoked whenever the component is updated/dependency array changes.
  • deps: Array: The dependency array

Submission

Start the timer, complete your solution, test your solution against the test cases provided by the platform, and submit it. Ideally, you should finish this question within 30 mins.

Share your solution with us on twitter or linkedin