question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Argument of type 'void' is not assignable to parameter of type '(state: AppStateStatus) => void'

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
editstudiocommented, Apr 6, 2022

How should this work for react-native 0.65 where removeEventListener has been depreciated, but AppState.addEventListener return void?

1reaction
ernstluringcommented, Nov 15, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Argument of type 'void' is not assignable to parameter ...
First of all let's look at SetStateAction<never[]> . If you hover over serverData in this line: const [serverData, serverDataLoaded] ...
Read more >
Argument type 'void' is not assignable to parameter of type
The error message "Argument of type 'void' is not assignable to parameter of type" means that we are passing an argument of type...
Read more >
Incorrect "Type void is not assignable to ...
If I add a parameter to the function (like so: e => scriptEvent(); ), and do e. , WebStorm suggests function functions &...
Read more >
type '() => void' is not assignable to type 'reactnode'
Solution for “Type 'void' is not assignable to type” error in TypeScript Return value for function Change type in the variable Summary The...
Read more >
[Solved]-TypeScript error: Type 'void' is not assignable to type ...
currentTarget.value); } } //setName doesn't receive parameters <Input onChange={(e) => validateStringLength(e, setName)} value={name} />.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found