marudy/react-native-responsive-screen

ERROR TypeError: Cannot read property 'setState' of undefined, js engine: hermes

getsettalk opened this issue · 4 comments

Hi when i going to watch our device oriantion than showing this error :

ERROR TypeError: Cannot read property 'setState' of undefined, js engine: hermes

Here is code 🤞 :

   useEffect(() => {
    lor(); // Start listening for orientation changes
    
    return () => {
      rol(); // Stop listening when the component unmounts
    };
  }, []); 

this is screenshots:

image
image

please fix as soon as possiable

Hello @getsettalk
I am not working on the library, I just want to help

If you check the code for the "listenOrientationChange" function, you will see that it's designed to be only used with a class based components and it relays on the value of "this" to be passed as an argument to call setState to update the component when the orientation changes

Check the provided example here https://github.com/marudy/react-native-responsive-screen/tree/master/examples/responsive-screen-orientation-change

My suggestion to solve the issue, is that you use the new hook introduced by react native "useWindowDimensions", you can just call it inside your functional component and ignore the return (since the width and height are handled by this library), and it should automatically render your component when orientation changes, and will also do the required cleanup on unmounting
Also keep in mind that you will need to move your styles inside the component function so that the "hp" and "wp" are re-calculated when orientation changes.

Hope that helps

@getsettalk
As i said, I am not a developer for this library, I just wanted to provide a solution for your question

I would be happy to submit a PR to update this library, but It seems no one is maintaining it and the PR won't be submitted.