Comments (13)
this is a component:
function MyComponent(){
}
this is a react element:
const element = <MyComponent/>
and it is equivalent to
const element = React.createElement(MyComponent, {})
you probably pass <SomeComponent/>
to a component that you expect wont re-render.
and it creates a new element each time, right?
from why-did-you-render.
exactly
from why-did-you-render.
so your component will re-render every time even if it's pure because
<SomeComponent/> !== <SomeComponent/>
an this is always true.
what you can do is to wrap the both of the components in a pure component
class TwoComponents extends React.PureComponent{
render() {
return (
<ComponentA b={<ComponentB/>}/>
)
}
}
and then both of them will only re-render if the props of TwoComponents
changes
from why-did-you-render.
I already have all components extending PureComponent, that is why I don't understand why is this happening, also I have not implemented shouldComponentUpdate or anything like that.
interface Props {
className ?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
}
export default class Li extends React.PureComponent<Props> {
render(): React.ReactNode {
return (
<li
className={this.extendClassName()}
children={this.props.children}
style={this.props.style}
/>
);
}
}
This an example of a component that I have, that always re-rendering
from why-did-you-render.
what is passed to Li?
What's re-rendered all the time?
from why-did-you-render.
<ul>
<Li className="a-class">
<img src="logo.png" />
</Li>
</ul>
If had some custom element inside I would undertand why it is re-rendering, but I have an image, and on other Li only have plain text
from why-did-you-render.
Ok. so <img/>
is a new react-element on every render.
This is why Li
cant be pure.
if you add shouldComponentUpdate
you will see this.props.children !== nextProps.children
from why-did-you-render.
yes this.props.children !== nextProps.children
always returns true.
then, if a component of my mine has children, then, do they must be a Component instead PureComponent?
from why-did-you-render.
yes. their children is always new so they will always re-render even if they are pure.
from why-did-you-render.
I'll add it to the guide
from why-did-you-render.
thank you.
I will have to organize better my components, to prevent unnecesary re-renderings
from why-did-you-render.
This library was design to catch these types of errors so I'm happy it helped you :)
from why-did-you-render.
updated http://bit.ly/wdyr02
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.