Skip to content

Commit 8da3dbd

Browse files
authored
Fix/event listener warning (#1605)
* Upgrade to rn65 * return jcenter * Fix usage of Dimensions.removeEventListener for RN65 * Update button snapshot tests * Remove orientation listener from Dialog
1 parent ae996ae commit 8da3dbd

File tree

14 files changed

+29
-37
lines changed

14 files changed

+29
-37
lines changed

demo/src/screens/componentScreens/CarouselScreen.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ interface State {
3636

3737
class CarouselScreen extends Component<Props, State> {
3838
carousel = React.createRef<typeof Carousel>();
39+
private dimensionsChangeListener: any;
3940

4041
constructor(props: Props) {
4142
super(props);
@@ -51,11 +52,11 @@ class CarouselScreen extends Component<Props, State> {
5152
}
5253

5354
componentDidMount() {
54-
Constants.addDimensionsEventListener(this.onOrientationChange);
55+
this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChange);
5556
}
5657

5758
componentWillUnmount() {
58-
Constants.removeDimensionsEventListener(this.onOrientationChange);
59+
Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChange);
5960
}
6061

6162
onOrientationChange = () => {

generatedTypes/src/components/button/index.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ declare class Button extends PureComponent<Props, ButtonState> {
154154
};
155155
};
156156
componentDidUpdate(prevProps: Props): void;
157-
componentDidMount(): void;
158-
componentWillUnmount(): void;
159-
onOrientationChanged: () => void;
160157
onLayout: (event: LayoutChangeEvent) => void;
161158
get isOutline(): boolean;
162159
get isFilled(): boolean;

generatedTypes/src/components/carousel/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare class Carousel extends Component<CarouselProps, CarouselState> {
2020
orientationChange?: boolean;
2121
skippedInitialScroll?: boolean;
2222
isAutoScrolled: boolean;
23+
private dimensionsChangeListener;
2324
constructor(props: CarouselProps);
2425
static getDerivedStateFromProps(nextProps: CarouselProps, prevState: CarouselState): {
2526
pageWidth: number;

generatedTypes/src/components/gridView/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ declare class GridView extends UIComponent<GridViewProps, State> {
4848
itemSpacing: number;
4949
};
5050
private itemSize?;
51+
private dimensionsChangeListener;
5152
constructor(props: ExistProps);
5253
static getDerivedStateFromProps(nextProps: ExistProps, prevState: State): {
5354
viewWidth: number;

generatedTypes/src/components/slider/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export default class Slider extends PureComponent<SliderProps, SliderState> {
124124
private containerSize;
125125
private trackSize;
126126
private thumbSize;
127+
private dimensionsChangeListener;
127128
constructor(props: SliderProps);
128129
checkProps(props: SliderProps): void;
129130
getAccessibilityProps(): {

generatedTypes/src/components/wizard/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare class Wizard extends Component<WizardProps, State> {
1818
static displayName: string;
1919
static Step: typeof WizardStep;
2020
static States: typeof WizardStepStates;
21+
private dimensionsChangeListener;
2122
constructor(props: WizardProps);
2223
componentDidMount(): void;
2324
componentWillUnmount(): void;

src/components/button/index.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,6 @@ class Button extends PureComponent<Props, ButtonState> {
4343
}
4444
}
4545

46-
componentDidMount() {
47-
Constants.addDimensionsEventListener(this.onOrientationChanged);
48-
}
49-
50-
componentWillUnmount() {
51-
Constants.removeDimensionsEventListener(this.onOrientationChanged);
52-
}
53-
54-
onOrientationChanged = () => {
55-
if (Constants.isTablet && this.props.fullWidth) {
56-
// only to trigger re-render
57-
this.setState({isLandscape: Constants.isLandscape});
58-
}
59-
};
60-
6146
// This method will be called more than once in case of layout change!
6247
onLayout = (event: LayoutChangeEvent) => {
6348
const height = event.nativeEvent.layout.height;

src/components/carousel/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
4545
orientationChange?: boolean;
4646
skippedInitialScroll?: boolean;
4747
isAutoScrolled: boolean;
48+
private dimensionsChangeListener: any;
4849

4950
constructor(props: CarouselProps) {
5051
super(props);
@@ -106,15 +107,15 @@ class Carousel extends Component<CarouselProps, CarouselState> {
106107
}
107108

108109
componentDidMount() {
109-
Constants.addDimensionsEventListener(this.onOrientationChanged);
110+
this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChanged);
110111

111112
if (this.props.autoplay) {
112113
this.startAutoPlay();
113114
}
114115
}
115116

116117
componentWillUnmount() {
117-
Constants.removeDimensionsEventListener(this.onOrientationChanged);
118+
Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChanged);
118119

119120
if (this.autoplayTimer) {
120121
clearInterval(this.autoplayTimer);

src/components/colorPicker/ColorPalette.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ class ColorPalette extends PureComponent<Props, State> {
110110
usePagination?: boolean = undefined;
111111
innerMargin?: number = undefined;
112112
swatchStyles?: StyleProp<ViewStyle>[] = undefined;
113+
private dimensionsChangeListener: any;
113114

114115
componentDidMount() {
115-
Constants.addDimensionsEventListener(this.onOrientationChanged);
116+
this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChanged);
116117
}
117118

118119
componentWillUnmount() {
119-
Constants.removeDimensionsEventListener(this.onOrientationChanged);
120+
Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChanged);
120121
}
121122

122123
onOrientationChanged = () => {

src/components/gridView/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class GridView extends UIComponent<GridViewProps, State> {
6262
};
6363

6464
private itemSize?: number;
65+
private dimensionsChangeListener: any;
6566

6667
constructor(props: ExistProps) {
6768
super(props);
@@ -92,11 +93,11 @@ class GridView extends UIComponent<GridViewProps, State> {
9293
}
9394

9495
componentDidMount() {
95-
Constants.addDimensionsEventListener(this.onOrientationChanged);
96+
this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChanged);
9697
}
9798

9899
componentWillUnmount() {
99-
Constants.removeDimensionsEventListener(this.onOrientationChanged);
100+
Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChanged);
100101
}
101102

102103
onOrientationChanged = () => {

src/components/slider/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export default class Slider extends PureComponent<SliderProps, SliderState> {
162162
private containerSize: Measurements | undefined;
163163
private trackSize: Measurements | undefined;
164164
private thumbSize: Measurements | undefined;
165+
private dimensionsChangeListener: any;
165166

166167
constructor(props: SliderProps) {
167168
super(props);
@@ -225,11 +226,11 @@ export default class Slider extends PureComponent<SliderProps, SliderState> {
225226
}
226227

227228
componentDidMount() {
228-
Constants.addDimensionsEventListener(this.onOrientationChanged);
229+
this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChanged);
229230
}
230231

231232
componentWillUnmount() {
232-
Constants.removeDimensionsEventListener(this.onOrientationChanged);
233+
Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChanged);
233234
}
234235

235236

src/components/tabController/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function TabController({
5959
}: PropsWithChildren<TabControllerProps>) {
6060
const [screenWidth, setScreenWidth] = useState<number>(Constants.windowWidth);
6161
const orientation = useRef<orientations>(Constants.orientation);
62+
const dimensionsChangeListener = useRef<any>();
6263

6364
if (items?.length < 2) {
6465
console.error('TabController component expect a minimum of 2 items');
@@ -71,9 +72,9 @@ function TabController({
7172
setScreenWidth(Constants.windowWidth);
7273
}
7374
};
74-
Constants.addDimensionsEventListener(onOrientationChange);
75+
dimensionsChangeListener.current = Constants.addDimensionsEventListener(onOrientationChange);
7576
return () => {
76-
Constants.removeDimensionsEventListener(onOrientationChange);
77+
Constants.removeDimensionsEventListener(dimensionsChangeListener.current || onOrientationChange);
7778
};
7879
}, []);
7980

src/components/wizard/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Wizard extends Component<WizardProps, State> {
2828
static Step: typeof WizardStep;
2929
static States: typeof WizardStepStates;
3030

31+
private dimensionsChangeListener: any;
32+
3133
constructor(props: WizardProps) {
3234
super(props);
3335

@@ -37,11 +39,11 @@ class Wizard extends Component<WizardProps, State> {
3739
}
3840

3941
componentDidMount() {
40-
Constants.addDimensionsEventListener(this.onOrientationChange);
42+
this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChange);
4143
}
4244

4345
componentWillUnmount() {
44-
Constants.removeDimensionsEventListener(this.onOrientationChange);
46+
Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChange);
4547
}
4648

4749
onOrientationChange = () => {

src/helpers/Constants.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,12 @@ const constants = {
127127
},
128128
/* Orientation */
129129
addDimensionsEventListener: (callback: any) => {
130-
Dimensions.addEventListener('change', callback);
130+
return Dimensions.addEventListener('change', callback);
131131
},
132132
/* Dimensions */
133133
removeDimensionsEventListener: (callback: any) => {
134-
// @ts-expect-error
135-
if (Dimensions.remove) {
136-
// @ts-expect-error
137-
Dimensions.remove('change', callback);
134+
if (callback.remove) {
135+
callback.remove();
138136
} else {
139137
Dimensions.removeEventListener('change', callback);
140138
}

0 commit comments

Comments
 (0)