Skip to content

Commit

Permalink
Compatible with react native >= 0.65 [Deprecated Dimensions event lis…
Browse files Browse the repository at this point in the history
…teners](mochixuan#50)
  • Loading branch information
flyskywhy committed Sep 19, 2024
1 parent 9750ccb commit 8176f89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
17 changes: 15 additions & 2 deletions lib/modaltoast/ModalToastView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from 'react-native'
import {ToastInOutDuration, ToastInHeight} from '../data/Constants'

const ReactNativeVersion = require('react-native/Libraries/Core/ReactNativeVersion');

const MaxWidthRatio = 0.8
export default class ModalToastView extends Component{

Expand All @@ -25,7 +27,14 @@ export default class ModalToastView extends Component{
}

// React after 17
Dimensions.addEventListener('change', this.onWindowChange);
if (ReactNativeVersion.version.minor >= 65) {
this.dimensionsSubscription = Dimensions.addEventListener(
"change",
this.onWindowChange
);
} else {
Dimensions.addEventListener('change', this.onWindowChange);
}
}

componentDidMount() {
Expand All @@ -37,7 +46,11 @@ export default class ModalToastView extends Component{
this.liftCycleAnimated.stop()
this.liftCycleAnimated = undefined
}
Dimensions.removeEventListener('change', this.onWindowChange);
if (ReactNativeVersion.version.minor >= 65) {
this.dimensionsSubscription?.remove();
} else {
Dimensions.removeEventListener('change', this.onWindowChange);
}
}

render() {
Expand Down
19 changes: 16 additions & 3 deletions lib/snackbar/SnackView.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import {SnackBarDuration, SnackBarInOutDuration, SnackBarPosition} from '../data/Constants'
import {isIphoneX,iosStatusBarXHeight,iosBottomXHeight,defaultStatusBarHeight, shadowBlackstyleTop,shadowBlackStyleBottom} from "../util/UiUtil";

const ReactNativeVersion = require('react-native/Libraries/Core/ReactNativeVersion');

class SnackView extends Component{

constructor(props) {
Expand All @@ -25,7 +27,14 @@ class SnackView extends Component{
}

// React after 17
Dimensions.addEventListener('change', this.onWindowChange);
if (ReactNativeVersion.version.minor >= 65) {
this.dimensionsSubscription = Dimensions.addEventListener(
"change",
this.onWindowChange
);
} else {
Dimensions.addEventListener('change', this.onWindowChange);
}

this._panResponder = PanResponder.create({
onStartShouldSetPanResponderCapture: (evt, gestureState) => false,
Expand Down Expand Up @@ -144,7 +153,7 @@ class SnackView extends Component{
} else if (this.props.position === SnackBarPosition.BOTTOM && gestureState.vy > 0.2) {
this.onHideAnimated(true)
}
}
}
}

onLifeCycleManage = () => {
Expand Down Expand Up @@ -227,7 +236,11 @@ class SnackView extends Component{
this.hideAnimated.stop()
this.hideAnimated = undefined
}
Dimensions.removeEventListener('change', this.onWindowChange);
if (ReactNativeVersion.version.minor >= 65) {
this.dimensionsSubscription?.remove();
} else {
Dimensions.removeEventListener('change', this.onWindowChange);
}
}

getStatusBarHeight = () => {
Expand Down
19 changes: 16 additions & 3 deletions lib/toast/ToastView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import {ToastInOutDuration, ToastInHeight} from '../data/Constants'
import { shadowBlackStyleBottom } from '../util/UiUtil'

const ReactNativeVersion = require('react-native/Libraries/Core/ReactNativeVersion');

const MaxWidthRatio = 0.8
export default class ToastView extends Component{

Expand All @@ -25,7 +27,14 @@ export default class ToastView extends Component{
}

// React after 17
Dimensions.addEventListener('change', this.onWindowChange);
if (ReactNativeVersion.version.minor >= 65) {
this.dimensionsSubscription = Dimensions.addEventListener(
"change",
this.onWindowChange
);
} else {
Dimensions.addEventListener('change', this.onWindowChange);
}
}

componentDidMount() {
Expand All @@ -37,7 +46,11 @@ export default class ToastView extends Component{
this.liftCycleAnimated.stop()
this.liftCycleAnimated = undefined
}
Dimensions.removeEventListener('change', this.onWindowChange);
if (ReactNativeVersion.version.minor >= 65) {
this.dimensionsSubscription?.remove();
} else {
Dimensions.removeEventListener('change', this.onWindowChange);
}
}

render() {
Expand Down Expand Up @@ -81,7 +94,7 @@ export default class ToastView extends Component{

let shadowStyle = {};
if (this.props.isShowShadow) shadowStyle = shadowBlackStyleBottom;


return (
<View style={[styles.container,containerStyle]} pointerEvents="none">
Expand Down

0 comments on commit 8176f89

Please sign in to comment.