Skip to content

Add option to ignore specified props in shallowCompare #6784

@fredrikolovsson

Description

@fredrikolovsson

[feature request/question]
Consider a component that has a stateless functional component (SFC) as prop, e.g. like this:

<MyComponent component={(props) => <div>{props.title}</div>;} />

Using shallowCompare in shouldComponentUpdate of MyComponent would always return true, but there are cases when it makes sense to use shallowCompare on all props except those with SFCs, like in this issue in redux-form, which had this solution:

shouldComponentUpdate(nextProps) {
  const propsWithoutComponent = { ...this.props }
  const nextPropsWithoutComponent = { ...nextProps }
  delete propsWithoutComponent.component
  delete nextPropsWithoutComponent.component
  return shallowCompare({ props: propsWithoutComponent }, nextPropsWithoutComponent)
}

While I have no idea how common this pattern is or will be, I saw that context is about to be added to shallowCompare and I thought maybe it would be possible to add an option to make shallowCompare ignore one or several props?

E.g. the signature could be like this instead:

function shallowCompare(instance, { nextProps, nextState, nextContext, propsToIgnore })

where propsToIgnore could be an array of string keys. (To avoid a breaking change, propsToIgnore could of course be added as the fifth argument instead, but this seems more robust to me.)

If this makes sense to you, I could have a go at a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions