Code Monkey home page Code Monkey logo

Comments (13)

vzaidman avatar vzaidman commented on May 16, 2024

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.

xgc1986 avatar xgc1986 commented on May 16, 2024

exactly

from why-did-you-render.

vzaidman avatar vzaidman commented on May 16, 2024

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.

xgc1986 avatar xgc1986 commented on May 16, 2024

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.

vzaidman avatar vzaidman commented on May 16, 2024

what is passed to Li?
What's re-rendered all the time?

from why-did-you-render.

xgc1986 avatar xgc1986 commented on May 16, 2024
    <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.

vzaidman avatar vzaidman commented on May 16, 2024

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.

xgc1986 avatar xgc1986 commented on May 16, 2024

yes this.props.children !== nextProps.childrenalways 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.

vzaidman avatar vzaidman commented on May 16, 2024

yes. their children is always new so they will always re-render even if they are pure.

from why-did-you-render.

vzaidman avatar vzaidman commented on May 16, 2024

I'll add it to the guide

from why-did-you-render.

xgc1986 avatar xgc1986 commented on May 16, 2024

thank you.

I will have to organize better my components, to prevent unnecesary re-renderings

from why-did-you-render.

vzaidman avatar vzaidman commented on May 16, 2024

This library was design to catch these types of errors so I'm happy it helped you :)

from why-did-you-render.

vzaidman avatar vzaidman commented on May 16, 2024

updated http://bit.ly/wdyr02

from why-did-you-render.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.