marudy / react-native-responsive-screen

Make React Native views responsive for all devices with the use of 2 simple methods
MIT License
1.55k stars 140 forks source link

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

Open getsettalk opened 11 months ago

getsettalk commented 11 months ago

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

mohamedatef98 commented 10 months ago

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 commented 10 months ago

Thank You , but you should update is it's very helpful

On Wed, 18 Oct 2023, 2:45 am mohamedatef98, @.***> wrote:

Hello @getsettalk https://github.com/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

— Reply to this email directly, view it on GitHub https://github.com/marudy/react-native-responsive-screen/issues/156#issuecomment-1767198682, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNAOH7EKTJE5VUUCOF3X73YPFAVCNFSM6AAAAAA5EXCH76VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRXGE4TQNRYGI . You are receiving this because you were mentioned.Message ID: @.***>

mohamedatef98 commented 10 months ago

@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.

getsettalk commented 10 months ago

Oh , than you can make a new npm package with thats code ... And I will use your package

On Wed, 18 Oct 2023, 4:58 pm mohamedatef98, @.***> wrote:

@getsettalk https://github.com/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.

— Reply to this email directly, view it on GitHub https://github.com/marudy/react-native-responsive-screen/issues/156#issuecomment-1768251035, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNGEHODN2NGLDWRXIR3X764OLAVCNFSM6AAAAAA5EXCH76VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRYGI2TCMBTGU . You are receiving this because you were mentioned.Message ID: @.***>