khevamann / rn-responsive-styles

Responsive styles for react-native and react-native-web
MIT License
40 stars 3 forks source link

Fix crashes on React Native version ^0.66 on refresh #11

Closed mtourj closed 2 years ago

mtourj commented 2 years ago

I recently upgraded my project from React Native 0.64.x to 0.68.x, as many other Expo users who have decided to upgrade to SDK 45 may have.

There seems to be an issue in React Native concerning the return value of useEffect handlers, and if it doesn't like the return type it just throws a cryptic error saying:

Warning: Internal React error: Attempted to capture a commit phase error inside a detached tree. This indicates a bug in React. Likely causes include deleting the same fiber more than once, committing an already-finished tree, or an inconsistent return pointer. 

Error message:

%s%s, TypeError: undefined is not an object (evaluating 'this.emitter'), 
    in Partnerships (created by ProfileScreen)
    in RCTView (created by View)
   ...

image

Anyway, after a quick process of elimination I figured out the source of problem was the return value of the useEffect function of this package. After changing the return value from an expression that returns undefined | () => void to () => void, the errors stopped appearing and the crashes ceased.

It is a quick and painless 3-liner fix, and even though you've got react-native 0.63 in your dependencies, I thought this might be a good quality-of-life contribution :).

What is interesting is that this was actually causing a crashing on every fast-refresh, although in other cases when I got the same error due to the same thing in my own useEffect functions, it did not crash my app. It's pretty cryptic so I don't even know where to begin, but at least I managed to fix the errors.