Skip to content

Commit

Permalink
Fixed marudy#21
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedMKamal committed Mar 10, 2019
1 parent 3d620a6 commit 6c8cd2f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
7 changes: 5 additions & 2 deletions examples/responsive-screen-orientation-change/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {

export default class App extends React.Component {
componentWillMount() {
listenOrientationChange(this);
// `listenOrientationChange` returns the handler method,
// consider store it to be pushed back when removing the listener.
this.orientationChangeHandler = listenOrientationChange(this);
}

componentWillUnMount() {
removeOrientationListener();
// Optionally path the original handler to be removed.
removeOrientationListener(this.orientationChangeHandler);
}

render() {
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/responsive-screen-orientation-change/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"expo": "^27.0.1",
"react": "16.3.1",
"react-native": "~0.55.2",
"react-native-responsive-screen": "^1.1.2"
"react-native-responsive-screen": "file:./local-package/react-native-responsive-screen-1.3.0-preview.tgz"
}
}
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ declare module 'react-native-responsive-screen' {

export function widthPercentageToDP(widthPercent: string | number): number;
export function heightPercentageToDP(widthPercent: string | number): number;
export function listenOrientationChange(screenClassComponent: Component<any, any>): void;
export function removeOrientationListener(): void;
export function listenOrientationChange(screenClassComponent: Component<any, any>): (newDimensions: any) => void;
export function removeOrientationListener(orientationChangeHandler?: any): void;
}
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,21 @@ const heightPercentageToDP = heightPercent => {
* invoke setState method and trigger screen rerender (this.setState()).
*/
const listenOrientationChange = that => {
Dimensions.addEventListener('change', newDimensions => {
const orientationChangeHandler = newDimensions => {
// Retrieve and save new dimensions
screenWidth = newDimensions.window.width;
screenHeight = newDimensions.window.height;

// Trigger screen's rerender with a state update of the orientation variable
that.setState({
orientation: screenWidth < screenHeight ? 'portrait' : 'landscape'
});
});
}

Dimensions.addEventListener('change', orientationChangeHandler);

// Save this somewhere to path it back when removing the listener.
return orientationChangeHandler;
};

/**
Expand All @@ -65,7 +70,11 @@ const listenOrientationChange = that => {
* listenOrientationChange function has been invoked. This should be done in order to
* avoid adding new listeners every time the same component is re-mounted.
*/
const removeOrientationListener = () => {
const removeOrientationListener = orientationChangeHandler => {
// Remove the original handler if possible, otherwise path an empty function.
if (orientationChangeHandler) {
return Dimensions.removeEventListener('change', orientationChangeHandler);
}
Dimensions.removeEventListener('change', () => {});
};

Expand Down

0 comments on commit 6c8cd2f

Please sign in to comment.