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 141 forks source link

add currentOrientation(), a new example using hooks & upgrade all examples to expo v38 #83

Open gregfenton opened 4 years ago

gregfenton commented 4 years ago

In trying to get a hooks example going, I ended up doing a fair amount of work I hope others might benefit from:

gregfenton commented 4 years ago

One note: in each of the examples' package.json, the dependency for react-native-responsive-screen points to a version using yalc because the examples requires a version that isn't published yet (it is part of this PR...circular dependency).

To engage with this version, you can follow these steps:

  1. Clone the pull request to your machine
    • git clone https://github.com/marudy/react-native-responsive-screen.git
    • cd react-native-responsive-screen
    • git fetch origin pull/83/head
    • git checkout -b pullrequest FETCH_HEAD
  2. Install yalc: npm install yalc (or yarn add yalc)
  3. "Publish" the project locally: yalc publish

Now you are ready to run any of the examples. Let's try the new Hook-based one:

  1. cd examples/responsive-screen-orientation-change-with-hooks
  2. npm install
  3. yalc add react-native-responsive-screen
  4. yalc update react-native-responsive-screen << not sure why this was necessary, but running failed to find the NPM without it
  5. npm run ios
  6. Dance a little dance! 🕺
gregfenton commented 4 years ago

If not obvious (it is quite a large PR), the likely controversial parts of this are:

  1. Move to use of Expo. The thinking here is that if it works with Expo, then it works with React Native. The converse is not necessarily true. And with Expo there are far less "moving parts" for simply getting up and running.

  2. The method signature of listenOrientationChange has changed, forcing the developer to identify whether they are passing in a class component's "this", or a functional component's "set" method from a call to useState() -- though the code does not enforce that function be a useState() function, so you could pass in any type of function.

herenickname commented 3 years ago

Can I use your fork in a production?

gregfenton commented 3 years ago

It is warranted for its full price 🙂

I am using it in production.

Yhozen commented 3 years ago

@gregfenton you could as well add hook support to currentOrientation, I can help if you'd like