use-what-changed
A simple hook to debug major Reactjs hooks and custom hooks.

Install
If you use yarn. Run
yarn add use-what-changed
If you use npm. Run
npm i use-what-changed --save
Motivation
I have been working on hooks for quite a long time. I use react hooks every day in my open source projects and also at work.
Now, using useEffect, useCallback, useMemo have really helped me compose the logic well together. But when the dependency list gets long. When I say long , it can be any thing greater than 3 for me and can be more or less for others.
With these large dependency array, I found it really difficult to debug and find out what is causing my useEffect to run again( same for useCallback and useMemo). I know two strategies to debug:
- Break the useEffect logic into multiple useEffect. It is still fine, but expertise and time constraints will be there. People will not break the useEffect logic into smaller pieces first, they will try to spend time using logging the values and adding debugger so that not to change the production code.
- Make use of usePrevious hook which can be defined something like this
import React from 'react';
function usePrevious(value) {
const ref = React.useRef(value);
React.useEffect(() => {
ref.current = value;
});
return ref.current;
}
export default usePrevious;
And can be consumed like this:
const previousA = usePrevious(a);
const previousB = usePrevious(b);
const previousC = usePrevious(c);
useEffect(() => {
if (previousA !== a) {
console.log(`a has changed from ${previousA} to ${a}`);
}
if (previousB !== b) {
console.log(`a has changed from ${previousB} to ${b}`);
}
if (previousC !== c) {
console.log(`a has changed from ${previousC} to ${c}`);
}
}, [a, b, c]);
However we can do it , it quite too much of work every time you run in the issue , where useEffect callback is running unexpectedly.
To solve the above problem, I tried to create something which can enhance developer experience in this case. Let's see my try for the above problems.
Usage
Note: This hook only logs in the development environment. It make use of standard process.env.NODE_ENV to decide. Open devtools console tab to see the logs.
- When only dependency are passed as the single argument
import useWhatChanged from 'use-what-changed';
function App() {
const [a, setA] = React.useState(0);
const [b, setB] = React.useState(0);
const [c, setC] = React.useState(0);
const [d, setD] = React.useState(0);
useWhatChanged([a, b, c, d]);
React.useEffect(() => {
}, [a, b, c, d]);
return <div className="container">Your app jsx</div>;
}

Above snapshot show the console log when b and c has changed in the above code example.
- Pass two arguments to useWhatChanged which makes it possible for useWhatChanged to log the names of the variables also.
useWhatChanged([a, b, c, d], 'a, b, c, d');

Color coding
A unique background color will be given to each title text. It helps us in recognising the specific effect when debugging. A unique id is also given to help the debugging further.

Demo link

Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
simbathesailor
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Contributors
Thanks goes to these wonderful people (emoji key):