Skip to content

PureComponent's shallowEqual #13468

@ruirhy

Description

@ruirhy
class App extends React.PureComponent {
  state={
    ele: {a:'1'}
  }
  trigger=()=>{
    console.log('click')
    this.setState({ele: {a:'1'}})
  }
  render() {
    console.log('render')
    return (
      <div onClick={this.trigger}>click here to trigger setState</div>
    );
  }
}

A simple demo shows that click event triggers render every time.
demo

However, according to source code, React.PureComponent’s shouldComponentUpdate() shallowly compares the objects like following, the click event shouldn't trigger render.

Performs equality by iterating through keys on an object and returning false when any key has values which are not strictly equal between the arguments. Returns true when the values of all keys are strictly equal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions