Skip to content

Deep comparison in PureComponent question #3909

@RoyTinker

Description

@RoyTinker

Hello team,

We have a base class for React components that we're beginning to use. It's like PureComponent but does deep comparison of JSON-like values (just objects, arrays, and primitives). The purpose is to avoid re-renders when inputs haven't changed, even when objects/arrays were reconstructed.

import React from 'react';
import { compareObj } from './deepCompare';

export default class PureDeepComponent extends React.Component {
    shouldComponentUpdate(newProps, newState) {
        return !(compareObj(this.props, newProps) && compareObj(this.state, newState));
    }
}

I was surprised that something like this wasn't included in React, and I'm wondering what the team's thoughts are -- was there a good reason for not doing something like this?

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