Skip to content

Conversation

@retyui
Copy link
Contributor

@retyui retyui commented Oct 18, 2021

Summary

Issue: #243

// React Native 0.65.x +
typeof Dimensions.removeListener //function
Dimensions.removeListener(...) // print warning

Test Plan

What's required for testing (prerequisites)?

  • React Native >= 0.65

What are the steps to reproduce (after prerequisites)?

npx react-native init rn66
cd rn66
yarn add @react-native-community/hooks
# update App.js (see code below)
yarn start
yarn android # start project on android
# tap on "Toggle" button
// App.js

import React, {useState} from 'react';
import {Text, View, ScrollView, Button} from 'react-native';
import {
  useAccessibilityInfo,
  useAppState,
  useBackHandler,
  useCameraRoll,
  useClipboard,
  useDimensions,
  useImageDimensions,
  useKeyboard,
  useInteractionManager,
  useDeviceOrientation,
  useLayout,
} from '@react-native-community/hooks';

const img = {
  uri: 'https://occ-0-1068-92.1.nflxso.net/dnm/api/v6/E8vDc_W8CLv7-yMQu8KMEC7Rrr8/AAAABeV0Af4XqVIi8qSUEeV_llbkH9B-TyiTGukOX7pSFxAuAyoc9q-e--ErSFvK4dLjE7tYDAr1L0PXAja28cDsLWwGdA_A.jpg?r=43c',
};

function Hooks() {
  const a = {
    AccessibilityInfo: useAccessibilityInfo(),
    AppState: useAppState(),
    BackHandler: useBackHandler(() => {
      return true;
    }),
    CameraRoll: useCameraRoll(),
    Clipboard: useClipboard(),
    Dimensions: useDimensions(),
    ImageDimensions: useImageDimensions(img),
    Keyboard: useKeyboard(),
    InteractionManager: useInteractionManager(),
    DeviceOrientation: useDeviceOrientation(),
    Layout: useLayout(),
  };

  return <Text>{JSON.stringify(a, null, 2)}</Text>;
}

function App() {
  const [s, setS] = useState(true);

  return (
    <View padding={50} flex={1}>
      <Button title="Toggle" onPress={() => setS(p => !p)} />
      <ScrollView
        style={{
          borderWidth: 1,
          borderColor: 'red',
          flex: 1,
        }}>
        {s ? <Hooks /> : null}
      </ScrollView>
    </View>
  );
}

export default App;

Compatibility

OS Implemented
iOS
Android
Web

Checklist

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)
  • I've created a snack to demonstrate the changes: LINK HERE

@retyui
Copy link
Contributor Author

retyui commented Oct 18, 2021

useDeviceOrientation

 WARN  EventEmitter.removeListener('change', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115

useDimensions

 WARN  EventEmitter.removeListener('change', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:101

useAppState

 WARN  EventEmitter.removeListener('appStateDidChange', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:97

useAccessibilityInfo

 WARN  EventEmitter.removeListener('deviceEventName', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115
 WARN  EventEmitter.removeListener('deviceEventName', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115
 WARN  EventEmitter.removeListener('deviceEventName', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115
 WARN  EventEmitter.removeListener('deviceEventName', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115
 WARN  EventEmitter.removeListener('deviceEventName', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115
 WARN  EventEmitter.removeListener('deviceEventName', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 
Hooks@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.rn66&modulesOnly=false&runModule=true:109708:115

@retyui retyui changed the title Adapt unsubscription for new Event Emitter API [RN 0.65.x] Adapt unsubscription for new EventEmitter API Oct 18, 2021
Copy link
Member

@LinusU LinusU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks! 🙏

@LinusU LinusU added minor Increment the minor version when merged release Create a release when this pr is merged labels Oct 19, 2021
@LinusU LinusU merged commit 70504da into react-native-community:master Oct 19, 2021
@retyui retyui deleted the use-modern-event-emitter-api branch October 19, 2021 15:56
@pvinis
Copy link
Member

pvinis commented Oct 19, 2021

🚀 PR was released in v2.8.0 🚀

@pvinis pvinis added the released This issue/pull request has been released. label Oct 19, 2021
@KrisLau
Copy link

KrisLau commented Nov 11, 2021

Still getting the error for useKeyboard on react-native: 0.66.3

EventEmitter.removeListener('keyboardWillShow', ...): Method has been deprecated. Please instead use `remove()` on the subscription returned by `EventEmitter.addListener`. 

@retyui
Copy link
Contributor Author

retyui commented Nov 11, 2021

useKeyboard was modified in another PR: #244

@retyui
Copy link
Contributor Author

retyui commented Nov 11, 2021

As I understand RN team first, remove EventEmitter.removeListener but after time restore that method but added a warning

facebook/react-native@035718b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged release Create a release when this pr is merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants