Skip to content

InView always forces a React Tree Reconciliation #195

@etripier

Description

@etripier

The handleChange callback always calls setState regardless of whether or not the value for state.inView will change. This forces a React Tree Reconciliation for each observed component when the tree is mounted (IO calls the callback once for each observed component).

I think an optimization could be made to https://github.com/thebuilder/react-intersection-observer/blob/master/src/InView.tsx#L96:

handleChange = (inView: boolean, entry: IntersectionObserverEntry) => {
  if (inView !== this.state.inView || inView) {
    this.setState({ inView, entry })
  }
  if (this.props.onChange) {
    this.props.onChange(inView, entry)
  }
}

This would allow the child to receive a new value for entry when in view (where it counts), but would prevent the re-render/reconciliation that happens at the very beginning.

What do you think? Am I missing some context here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions