researchgate / react-intersection-observer

React component for the Intersection <Observer /> API

Home Page:https://researchgate.github.io/react-intersection-observer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle null children exception

opened this issue · comments

Thanks for taking the time to file an issue with us.
Please note that this issue template is used ONLY for reporting bugs.

If you have an issue that isn’t a bug, please follow the steps listed in the Contributing.
Thanks!

Expected behavior

If you pass as a child a React Component which could be null, this causes an unexpected TypeError.

Current behavior

Observer is passed the null instance and a TypeError occurs

Steps to reproduce

  1. Create an IntersectionObserver as a HOC and pass in a React Component (Class or SFC, returning null) as the only child
  2. Observe that the observer attempts to attach to whatever the React Component returns;
    image

Context (environment)

This got past our CI pipeline because TypeScript (correctly) allows null as a valid return type from React.SFC or React.ReactNode. I will try to take a look and provide my own solution, but please also advise if this is due to a limitation within React or otherwise.

  • Version: "0.7.3"
  • Platform: OSX Debian

Thanks for the detailed info. There are two ways we can tackle this, one is to not allow NULL and therefore modifying the TS types, and the other is to do no observation when a null type is passed. I believe the latter might bring less overhead to the implementor, since he/she won't have to account for it, specially if you're not using TS but PropTypes, where no compiler errors are enforced.

// This is too much overhead IMO
<Observer onChange={() => {/*...*/}}>
  {children == null ? <placeholder /> : children}
</Observer>

Sound good?

Sounds great. I agree with the latter suggestion

Do you feel confident enough to work on a PR for this? If not that's ok and I'll try to get it done soon.

I will give it a shot in the next few days

@311289189 see #52