Comments (10)
fixed in v5.0.0-alpha.2
from why-did-you-render.
after investigating this issue I believe I have the answers.
When you mark a "huge" component with "whyDidYouRender", you expect it to not re-render where it can be prevented.
While React.useCallback
is a very useful hook, it might cause problems nevertheless.
For example (i created a sandbox for this):
https://codesandbox.io/s/welldone-softwarewhy-did-you-render-usecallback-example-7co06
const UseCallbackNewFnOnClick = () => {
const [value, setValue] = React.useState(0);
// this useCallback returns a new function on each value change
const handleClick = React.useCallback(() => {
setValue(value + 1);
}, [value, setValue]);
return (
<div>
<h1>useCallback</h1>
value: {value}
<br />
<HugeComponentWithButtonWeDontWantToReRender
text="increase"
onClick={handleClick}
/>
</div>
);
};
vs
const UseCallbackAlwaysSameFn = () => {
const [value, setValue] = React.useState(0);
// this useCallback never returns a new function
const handleClick = React.useCallback(() => {
setValue(value => value + 1);
}, [setValue]);
return (
<div>
<h1>useCallback</h1>
value: {value}
<br />
<HugeComponentWithButtonWeDontWantToReRender
text="increase"
onClick={handleClick}
/>
</div>
);
};
If you mark a component with whyDidYouRender
, it probably means you don't want it to receive a new fn all the time. this can be prevented in many ways. one of them is the way displayed in the example, another one would be not to use hooks at all.
what do you think @DaleSalcedo ?
from why-did-you-render.
Related: facebook/react#14099
from why-did-you-render.
yes there are. I still think about how to make it as clear and as robust as possible in this sense. if you have any suggestions, i'll be glad to hear.
from why-did-you-render.
Re-opened for the record because I was asked about it again:
We wait on facebook/react#14099 to be fixed so the fn returned from useCallback
wan't cause a re-render of the component it is passed to.
from why-did-you-render.
This comparison was created before hooks were available, where passing () =>
to big components meant trouble. Now it's not the case however:
useCallback
regenerates the fn passed to a component only if it changed.
It's indeed something that we need to rethink.
from why-did-you-render.
Related: facebook/react#14099
exectly
from why-did-you-render.
Possible workaround: facebook/react#14099 (comment)
// use this instead of `useCallback`
function useEventCallback(fn) {
let ref = useRef();
useEffect(() => {
ref.current = fn;
});
return useCallback((...args) => ref.current(...args), []);
}
from why-did-you-render.
@vzaidman yes that makes alot of sense. thank you for the example as well.
i should probably close this issue now, thanks everyone for the feedback!
from why-did-you-render.
@vzaidman there are valid cases for callback invalidation, see facebook/react#14099 (comment)
How can I tell why-did-you-render
that callback reference change is expected here?
from why-did-you-render.
Related Issues (20)
- Integration with Next.js 13 app folder? HOT 1
- Issues with Parcel? HOT 3
- use React.lazy warp function, wdyr not work.
- Is it possible to print testID along side Component name on wdyr logs? HOT 1
- use jotai always diff
- dom npm
- wdyr not work in next13 (repo provided) HOT 2
- wdyr not work in next13 (repo provided)
- WDYR includes full lodash library with window._ override HOT 6
- Cannot get WDYR output from nextjs dynamically loaded component (e.g. for no-ssr use case) HOT 2
- Project still being actively maintained? HOT 2
- Multiple React roots
- why-did-you-render + React Query
- No logs or anything about WDYR HOT 9
- Missing `ownerDataMap` in `notifier`
- Partially Working with React 18 + NextJS 13
- "Support for defaultProps will be removed" error after setting include: [/./] option HOT 2
- Tracking custom hooks issue HOT 2
- WhyDidYouRender is not working in Storybook.js
- Property 'React' doesn't exist HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from why-did-you-render.