Gathering detailed insights and metrics for @simbathesailor/use-what-changed
Gathering detailed insights and metrics for @simbathesailor/use-what-changed
Gathering detailed insights and metrics for @simbathesailor/use-what-changed
Gathering detailed insights and metrics for @simbathesailor/use-what-changed
npm install @simbathesailor/use-what-changed
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
708 Stars
107 Commits
12 Forks
8 Watching
35 Branches
2 Contributors
Updated on 22 Nov 2024
Minified
Minified + Gzipped
TypeScript (86.34%)
CSS (10.67%)
HTML (2.98%)
Cumulative downloads
Total Downloads
Last day
-16.8%
10,751
Compared to previous day
Last week
-7.4%
61,488
Compared to previous week
Last month
10.8%
271,670
Compared to previous month
Last year
37.9%
3,116,216
Compared to previous year
1
useEffect | useCallback | useMemo | useLayoutEffect | Custom hooks using core hooks
Open the codesandbox link and see the console. You can uncomment the other hooks, and see the console accordingly, when the value changes across rerenders.
codesandbox use-what-changed example
If you use yarn. Run
1 2yarn add @simbathesailor/use-what-changed --dev 3
If you use npm. Run
npm i @simbathesailor/use-what-changed --save-dev
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:
1import React from 'react';
2
3function usePrevious(value) {
4 const ref = React.useRef(value);
5
6 React.useEffect(() => {
7 ref.current = value;
8 });
9
10 return ref.current;
11}
12
13export default usePrevious;
And can be consumed like this:
1const previousA = usePrevious(a); 2 3const previousB = usePrevious(b); 4 5const previousC = usePrevious(c); 6 7useEffect(() => { 8 if (previousA !== a) { 9 console.log(`a has changed from ${previousA} to ${a}`); 10 } 11 12 if (previousB !== b) { 13 console.log(`a has changed from ${previousB} to ${b}`); 14 } 15 16 if (previousC !== c) { 17 console.log(`a has changed from ${previousC} to ${c}`); 18 } 19}, [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.
Even if you are coming to your own code after days. It becomes very difficult to wrap you head around various multiple hooks . This library with babel plugin helps you to undersrtand it wiothout severe cognitive thinking
To solve all the above problems, I tried to create something which can enhance developer experience. Let's see how I tried to solve the problem.
The package can also be used with a babel plugin which make it more easy to debug.
npm i @simbathesailor/use-what-changed --save-dev
npm i @simbathesailor/babel-plugin-use-what-changed --save-dev
Add the plugin entry to your babel configurations
1{ 2 "plugins": [ 3 [ 4 "@simbathesailor/babel-plugin-use-what-changed", 5 { 6 "active": process.env.NODE_ENV === "development" // boolean 7 } 8 ] 9 ] 10}
Make sure the comments are enabled for your development build. As the plugin is solely dependent on the comments.
Now to debug a useEffect, useMemo or useCallback. You can do something like this:
1// uwc-debug 2React.useEffect(() => { 3 // console.log("some thing changed , need to figure out") 4}, [a, b, c, d]); 5 6// uwc-debug 7const d = React.useCallback(() => { 8 // console.log("some thing changed , need to figure out") 9}, [a, b, d]); 10 11// uwc-debug 12const d = React.useMemo(() => { 13 // console.log("some thing changed , need to figure out") 14}, [a]); 15 16// uwc-debug 17const d = React.useLayoutEffect(() => { 18 // console.log("some thing changed , need to figure out") 19}, [a]);
Notice the comments uwc-debug
in above examples. The comment uwc-debug
is responsible for the all the magic.
Notice the comments uwc-debug-below
below examples.
1React.useEffect(() => { 2 // console.log("some thing changed , need to figure out") 3}, [a, b, c, d]); 4 5// this comment enables tracking all the hooks below this line. so in this case useCallback and useMemo will be tracked. 6// uwc-debug-below 7const d = React.useCallback(() => { 8 // console.log("some thing changed , need to figure out") 9}, [a, b, d]); 10 11const d = React.useMemo(() => { 12 // console.log("some thing changed , need to figure out") 13}, [a]);
So the example will debug all the hooks below line containing // uwc-debug-below.
No need to add any import for use-what-changed. just add a comment uwc-debug or uwc-debug-below above your hooks and you should start seeing use-what-changed debug consoles. No more back and forth across files and browser, adding debuggers and consoles.
This plugin provides following information :
1. Hook name which it is debugging.
2. File name where hook is written
3. Name of dependencies passed to hook.
4. what has changed in dependency array which caused the re-run of hook with symbol icons ( ✅, ⏺).
5. Tells you old value and new value of all the dependencies.
6. Tells you whether it is a first run or an update. I found it very helpful in debugging cases.
7. Unique color coding and id for individual hooks for easy inspection
Note: Frankly speaking the whole package was built, cause I was facing problems with hooks and debugging it was eating up a lot of my time. Definitely using this custom hook with babel plugin have saved me a lot of time and also understand unknown edge cases while using hooks
1import {
2 useWhatChanged,
3 setUseWhatChange,
4} from '@simbathesailor/use-what-changed';
5
6// Only Once in your app you can set whether to enable hooks tracking or not.
7// In CRA(create-react-app) e.g. this can be done in src/index.js
8
9setUseWhatChange(process.env.NODE_ENV === 'development');
10
11// This way the tracking will only happen in devlopment mode and will not
12// happen in non-devlopment mode
13
14function App() {
15 const [a, setA] = React.useState(0);
16
17 const [b, setB] = React.useState(0);
18
19 const [c, setC] = React.useState(0);
20
21 const [d, setD] = React.useState(0);
22
23 // Just place the useWhatChanged hook call with dependency before your
24
25 // useEffect, useCallback or useMemo
26
27 useWhatChanged([a, b, c, d]); // debugs the below useEffect
28
29 React.useEffect(() => {
30 // console.log("some thing changed , need to figure out")
31 }, [a, b, c, d]);
32
33 return <div className="container">Your app jsx</div>;
34}
Above snapshot show the console log when b and c has changed in the above code example.
1useWhatChanged([a, b, c, d], 'a, b, c, d', 'anysuffix-string'); // debugs the below useEffect
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.
As this lbrary is just javascript and react. It can be used whereever Reactjs exists.
I have included the setup for elctron app with a repo example.
Todos: Need to add an example for react-native, which is work in progress. I will update it in a couple of days.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details
Thanks goes to these wonderful people (emoji key):
Anil kumar Chaudhary 💻 🤔 🎨 📖 🐛 |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/22 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
78 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More