Argument of type 'void' is not assignable to parameter of type '(state: AppStateStatus) => void'
See original GitHub issueDescription
I am trying to remove my AppState listener subscription, but I am getting the following es-lint warning:
Argument of type 'void' is not assignable to parameter of type '(state: AppStateStatus) => void'
I am just doing this:
useEffect(() => {
const unsubAppState = AppState.addEventListener(
"change",
handleOnAppStateChange
);
return () => {
AppState.removeEventListener("change", unsubAppState);
};
}, [handleOnAppStateChange, handleNetworkErrors]);
I know that the removeEventListener
method is deprecated, but the .remove()
is not a method of unsubAppState
… in fact, AppState.addEventListener
seems to be returning void
.
export interface AppStateStatic {
currentState: AppStateStatus;
/**
* Add a handler to AppState changes by listening to the change event
* type and providing the handler
*/
addEventListener(type: AppStateEvent, listener: (state: AppStateStatus) => void): void;
/**
* Remove a handler by passing the change event type and the handler
*/
removeEventListener(type: AppStateEvent, listener: (state: AppStateStatus) => void): void;
}
Version
0.64.3
Output of npx react-native info
System: OS: macOS 10.15.7 CPU: (8) x64 Intel® Core™ i5-1038NG7 CPU @ 2.00GHz Memory: 4.65 GB / 16.00 GB Shell: 5.7.1 - /bin/zsh Binaries: Node: 16.13.0 - /usr/local/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 8.4.0 - /usr/local/bin/npm Watchman: 2021.06.07.00 - /usr/local/bin/watchman Managers: CocoaPods: Not Found SDKs: iOS SDK: Platforms: iOS 14.4, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2 Android SDK: Not Found IDEs: Android Studio: Not Found Xcode: 12.4/12D4e - /usr/bin/xcodebuild Languages: Java: Not Found npmPackages: @react-native-community/cli: Not Found react: ^17.0.2 => 17.0.2 react-native: https://github.com/expo/react-native/archive/sdk-44.0.0.tar.gz => 0.64.3 react-native-macos: Not Found
Steps to reproduce
useEffect(() => {
const unsubAppState = AppState.addEventListener(
"change",
handleOnAppStateChange
);
return () => {
AppState.removeEventListener("change", unsubAppState);
};
}, [handleOnAppStateChange, handleNetworkErrors]);
Snack, code example, screenshot, or link to a repository
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (1 by maintainers)
How should this work for react-native 0.65 where
removeEventListener
has been depreciated, butAppState.addEventListener
return void?@VictorioMolina I had the exact same issue as you, but then I remembered I also needed to update the react-native typings to the correct version.