Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do PureComponents need props? #2186

Closed
ZYinMD opened this issue Mar 3, 2019 · 10 comments · Fixed by #2238
Closed

Why do PureComponents need props? #2186

ZYinMD opened this issue Mar 3, 2019 · 10 comments · Fixed by #2238

Comments

@ZYinMD
Copy link

ZYinMD commented Mar 3, 2019

I'm using "react/prefer-stateless-function": [1, { "ignorePureComponents": true }]

When I have a PureComponent that doesn't have props, it shows a warning.

However, the component below WILL re-render if the render() of its parent is called

const Child = () => <h1>I re-render when my parent does</h1>

You can see it with the React devtool in Chrome, turn on "highlight update"

If I change it to a PureComponent, it does not re-render:

class Child extends PureComponent {
  render() {
    return <h1>I don't</h1>;
  }
}

So to me, a PureComponent without props makes perfect sense.

@ZYinMD
Copy link
Author

ZYinMD commented Mar 3, 2019

For a quick test yourself, use this code:

import React, { Component, PureComponent } from 'react';

export default class App extends Component {
  state = {};

  handleClick = () => {
    this.setState({
      foo: 'bar'
    });
  }

  render() {
    return (
    <div>
      <button onClick={this.handleClick}>re-render parent</button>
      <Child1 />
      <Child1 />
      <Child1 />
      <Child1 />
      <Child2 />
      <Child2 />
      <Child2 />
      <Child2 />
    </div>
    );
  }
}

const Child1 = () => <h1>I re-render when my parent does</h1>;

class Child2 extends PureComponent {
  render() {
    return <h1>I don't</h1>;
  }
}

@ZYinMD ZYinMD changed the title Why do PureComponent need props? Why do PureComponents need props? Mar 3, 2019
@ljharb
Copy link
Member

ljharb commented Mar 3, 2019

Does the rule warn on Child2?

@ZYinMD
Copy link
Author

ZYinMD commented Mar 3, 2019

Yes.

@ljharb
Copy link
Member

ljharb commented Mar 3, 2019

With ignorePureComponents true, it’s not ignoring them, so that seems like a bug.

@ZYinMD
Copy link
Author

ZYinMD commented Mar 3, 2019

It was intended, so you could say it's not a bug, but it enforces something that's not ideal.

@ljharb
Copy link
Member

ljharb commented Mar 3, 2019

oh. good point. I’d expect react not to rerender a component that gets no props or context, but it might not have chosen to apply that optimization.

@alexzherdev
Copy link
Contributor

Perhaps on the off chance that it could depend on some global state?

@ljharb
Copy link
Member

ljharb commented Mar 5, 2019

If it does, react explicitly doesn't guarantee a rerender - only props and state do that.

@pawelnvk
Copy link
Contributor

Hopefully I understood you correctly. Please checkout: #2238.

@ljharb
Copy link
Member

ljharb commented Apr 12, 2019

Fixed by #2238.

@ljharb ljharb closed this as completed Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

4 participants