software-mansion / react-native-gesture-handler

Declarative API exposing platform native touch and gesture system to React Native.
https://docs.swmansion.com/react-native-gesture-handler/
MIT License
5.85k stars 954 forks source link

undefined is not an object (evaluating 'RNGestureHandlerModule.State') #205

Closed alz10 closed 5 years ago

alz10 commented 5 years ago

What i did

  1. npm install --save react-native-gesture-handler
  2. react-native link react-native-gesture-handler
  3. Edit my MainActivity
  4. Edit my MainApplication
  5. import { TapGestureHandler } from 'react-native-gesture-handler';
  6. Got the error below

34484119_1694071200628127_4496503740102082560_n

alz10 commented 5 years ago
"dependencies": {
    "@expo/vector-icons": "^6.3.1",
    "axios": "^0.18.0",
    "firebase": "^5.0.3",
    "react": "16.3.1",
    "react-native": "~0.55.2",
    "react-native-gesture-handler": "^1.0.4",
    "react-native-nested-scroll-view": "^6.0.1",
    "react-native-progress": "^3.4.0",
    "react-native-responsive-dimensions": "^1.0.2",
    "react-native-swiper": "^1.5.13",
    "react-native-text": "0.0.7",
    "react-native-vector-icons": "^4.6.0",
    "react-navigation": "^2.0.1",
    "watchman": "^1.0.0"
  }
victorporof commented 5 years ago

I had a similar issue. I'm using CocoaPods instead of react-native link.

Add this to your podfile:

pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler/ios'
alz10 commented 5 years ago

Hi i solved it now what i did was is to create a new RN project using react-native init nameOfYourProject and install the following:

npm install --save react-native-gesture-handler
react-native link react-native-gesture-handler

and edit my MainActivity and MainApplication:

android\app\src\main\java\com\nameOfYourProject\MainActivity.java

package com.mlguide; // make sure you change it to your project's name, mine is com.mlguide 

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "mlguide"; // this one too
    }

    @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
      return new ReactActivityDelegate(this, getMainComponentName()) {
        @Override
        protected ReactRootView createRootView() {
            return new RNGestureHandlerEnabledRootView(MainActivity.this);
      }
    };
  }
}

android\app\src\main\java\com\nameOfYourProject\MainApplication.java

package com.mlguide; // change it

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.oblador.vectoricons.VectorIconsPackage; // Remove this, this another library i use for my project
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new VectorIconsPackage(), // This one too
            new RNGestureHandlerPackage()
      );
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}
kmagiera commented 5 years ago

Thanks for reporting @Albert0405 it looks like you've missed some setup steps (namely link command). Since you managed to resolve it I'm going to close this issue. If you think k there is something we can do to make setup instructions cleaner feel free to start a new issue

victorporof commented 5 years ago

@kmagiera Lots of people integrate React Native into an existing project instead of starting from scratch. This often means using CocoaPods instead of react-native link. Some instructions for this use case would probably help immensely, especially since the Podfile isn't in the root directory.

kmagiera commented 5 years ago

Thanks @victorporof, I well understand the importance of supporting people who rely kn cocoapods for pulling in their dependencies. I just thought react-native link works with cocoapods (I think since RN 52) so didn't see it to be necessary to add that to instructions.

There is a PR open that adds podfile to the root folder. Would that help if we put it there under a "standardized" name?

alz10 commented 5 years ago

I would also like to suggest to update your documentation (Installation Guide). Make it beginner friendly. I was confused on Android part where you need to change some codes in it. It says:

Update your main activity (or wherever you create an instance of ReactActivityDelegate), 
so that it overrides the method responsible for creating ReactRootView instance. Then 
use a root view wrapper provided by this library:

import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

public class MainActivity extends ReactActivity {

  // Add the following method to your main activity class
  @Override
  protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
      @Override
      protected ReactRootView createRootView() {
        return new RNGestureHandlerEnabledRootView(MainActivity.this);
      }
    };
  }
}

I was like, What? Where can i find my main activity class? Im clueless so i googled it but it would be a time saver if you just include the location of that file just like what i did on my solution above

kmagiera commented 5 years ago

That's a great suggestions @Albert0405 would you be interested in updating the docs and sending a PR with that? If so I'd be more than happy to merge it

0xpatrickdev commented 5 years ago

Hey @kmagiera this was also a little unclear to me so I took a go at updating the docs

anchetaWern commented 5 years ago

I'm getting the same issue on iOS:

screen shot 2018-06-26 at 5 01 49 pm

This is after doing react-native-link. So I decided to use cocoapods instead:

cd ios
pod init

Then I added the following to my podfile:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'RNRealworldAnimations' do

  rn_path = '../node_modules/react-native'
  pod 'yoga', path: "#{rn_path}/ReactCommon/yoga/yoga.podspec"
  pod 'React', path: rn_path, subspecs: [
    'Core',
    'CxxBridge',
    'DevSupport',
    'RCTActionSheet',
    'RCTAnimation',
    'RCTGeolocation',
    'RCTImage',
    'RCTLinkingIOS',
    'RCTNetwork',
    'RCTSettings',
    'RCTText',
    'RCTVibration',
    'RCTWebSocket',
  ]

  pod 'DoubleConversion', :podspec => "#{rn_path}/third-party-podspecs/DoubleConversion.podspec"
  pod 'glog', :podspec => "#{rn_path}/third-party-podspecs/glog.podspec"
  pod 'Folly', :podspec => "#{rn_path}/third-party-podspecs/Folly.podspec"

  pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler/ios'
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == "React"
      target.remove_from_project
    end
  end
end

Executed pod install and ran the project again:

cd ..
react-native run-ios

Then I got the error:

`PickerIOS` does not exist in the Haste module map

Which was fixed by:

yarn start --reset-cache

But after that, I got the original error back.

Any ideas?

kmagiera commented 5 years ago

Sorry to hear it's happening to you too @anchetaWern

This problem has clearly to do with the library setup process and indicates that the native code is not being properly included and run in your app.

Unfortunately with the following details it is difficult for me to tell what's going wrong in your case. It seems like you should've run "pod install" or sth after adding stuff to Podfile (not sure if you did but it's not mentioned in your message).

The only advice that I have is to also check in other libraries as this problem is not specific to this one but rather to linking RN library with native code into your project in general. You could try adding some other library (such as react-native-permissions) and checking if it works. If you are familiar with xcode I'd suggest to look into the xcode project config and check if gesture handler library is added as dependency and being properly linked. If you are unfamiliar I suggest that you create a new RN project, link gesture handler with it and check if it works and if it does then try to find where the gesture handler library is listed in the project where it works vs your original project.

ikzjfr0 commented 5 years ago

I got the similar problem on iOS, i tried both way (link and cocoapod)

image

anchetaWern commented 5 years ago

@kmagiera I wasn't really sure how to check if a module is linked in Xcode, so what I did was to just start a new React Native project and copied the existing code over. I skipped react-native link and just proceeded to using the Podfile that I used above.

@ikzjfr0 maybe that will solve your issue too. But this method requires you to manually link on Android.

ikzjfr0 commented 5 years ago

@anchetaWern starting a new RN project does resolve the problem. However, my project is too big to easily just copying :)

gao520sun commented 5 years ago

Unhandled JS Exception: undefined is not an object (evaluating 'RNGestureHandlerModule.State')

casper123 commented 5 years ago

I was also getting the same issue. What I did was delete the app from my iPhone simulator and then go to Hardware -> Erase all content & settings. After that I reinstalled the app and everything worked fine after that :)

nshoes commented 5 years ago

@casper123 Wow! Haha that worked for me, thanks. I have a relatively small RN project, followed the first code example on the react-navigation website, and hit this error. react-native link did not help.

achmadk commented 5 years ago

Manual linking with Xcode work for me. You can drag RNGestureHandler.xcodeproj into Libraries, then drag libRNGestureHandler.a into Build Phases > Link Binary with Libaries

a-bayat commented 5 years ago

i have this issue and solve using :

harish-aka-shivi commented 5 years ago

I am using RNGestureHandlerEnabledRootView as my root view in Android Activity. Ideally it should work because createRootView() also returns the same, but its not and it is throwing same error. Is there anything I can do?

harish-aka-shivi commented 5 years ago

I am following these instructions to integrate react-native to my existing app.

harish-aka-shivi commented 5 years ago

I solved it by adding RNGestureHandlerPackage() to the ReactInstanceManagerBuilder's list of packages, manually. @kmagiera Is this something that can be added in the documentation, particularly, for the cases when developer is adding react-native to the existing native android apps? I am happy to help.

AntonioErdeljac commented 5 years ago

My solution was to wipe out emulator data or try react-native run-android on a completely new emulator. I installed and linked react-native-gesture-handler while my emulator was active and I thought re-running run-android would reset all cache but the emulator app needs to be removed and data wiped out and then try again. Hope this helps someone.

yangkezun commented 5 years ago

My solution was to wipe out emulator data or try react-native run-android on a completely new emulator. I installed and linked react-native-gesture-handler while my emulator was active and I thought re-running run-android would reset all cache but the emulator app needs to be removed and data wiped out and then try again. Hope this helps someone.

thanks

tandemloop commented 5 years ago

Did react-native link, wiped data, cleared cache, updated packages, no go. Just using the copied example in starting react-native docs. What fixed it was @achmadk suggested. The file already existed in Libraries but it didn't exist in Build Phases...

MIyulong commented 5 years ago

I had a similar issue. I'm using CocoaPods instead of react-native link.

Add this to your podfile:

pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler/ios'

as for me, it doesn't work, i get this [!] No podspec found for 'RNGestureHandler' in '../node_modules/react-native-gesture-handler/ios/', image

achmadk commented 5 years ago

Change pod path for RNGestureHandler to ../node_modules/react-native-gesture-handler, @MIyulong

pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler'
MIyulong commented 5 years ago

@achmadk Wow! Haha that worked for me, thanks.

acro5piano commented 5 years ago

I had the same problem in iOS, solved by manual installation:

I think it is helpful if this alternative procedure is written in the document of Getting Started > Installation section.

image 2018-12-26 13-11-05

hichamnaimi commented 5 years ago

If you are using react-link to link your dependencies: 1/ open your ios Podfile and delete all yout linked dependencies: pod 'xxxxxxx', :path => '../node_modules/xxxxx' 2/ Close Xcode 3/ In your /ios folder run "pod update" 4/ In your project source run "react-native link" 5/ Open Xcode and Clean Build Folder from Xcode Menu -> Product 6/ Run your application from Xcode 7/ Link manually the dependency "react-native-gesture-handler" into your Xcode Project following steps in documentation: https://facebook.github.io/react-native/docs/linking-libraries-ios 8/ Now run your application from Xcode, you should be fine.

cmcaboy commented 5 years ago

I was able to fix this by following @acro5piano's recommendation, but I had to also add libRNGestureHandler.a to General -> Linked Frameworks and Libraries section.

More or less, I had to manually link. react-native link did not work for my project.

Akashsystematix commented 5 years ago

Manual Mehod Will fix the issue.

lchenfox commented 5 years ago

I had a similar issue. I'm using CocoaPods instead of react-native link.

Add this to your podfile:

pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler/ios'

it works for me, thx

joydeep33 commented 5 years ago

I had a similar issue. I'm using CocoaPods instead of react-native link.

Add this to your podfile:

pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler/ios'

What is pod file

Aakash61212 commented 5 years ago

It is a used for adding third party

Refer https://cocoapods.org

KGDeveloper commented 5 years ago

手动链接Xcode为我工作。您可以拖动RNGestureHandler.xcodeproj到图书馆,然后拖动libRNGestureHandler.aBuild Phases > Link Binary with Libaries

Hello, I used this method, but still show this error

vinaygode commented 5 years ago
  1. (As first stated by @achmadk) Add to Podfile: pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler'
  2. It helped me to remove pods and reinstall: rm -rf Pods; pod install
andresmtz98 commented 5 years ago

Nothing from these workarounds work for me :/

Ovi commented 5 years ago

I was having same problem in Android Emulator Here's what I did

implementation project(':react-native-gesture-handler')

include ':react-native-gesture-handler' project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')

mdbaniani commented 5 years ago

If you are using react-link to link your dependencies: 1/ open your ios Podfile and delete all yout linked dependencies: pod 'xxxxxxx', :path => '../node_modules/xxxxx' 2/ Close Xcode 3/ In your /ios folder run "pod update" 4/ In your project source run "react-native link" 5/ Open Xcode and Clean Build Folder from Xcode Menu -> Product 6/ Run your application from Xcode 7/ Link manually the dependency "react-native-gesture-handler" into your Xcode Project following steps in documentation: https://facebook.github.io/react-native/docs/linking-libraries-ios 8/ Now run your application from Xcode, you should be fine.

after hours, only this worked for me

axdamx commented 5 years ago

@AmirhosseinBayat tricks does solved the problem! Thanks bro!

FadiAboMsalam commented 5 years ago

@achmadk finally after spending many hours linking manually solved the issue for me thanks a million

Dhirajbhujbal commented 5 years ago

Manual Integration of the Navigation project resolved this issue. 2 options we can use pods or we can do the manual integration.

PS: This will occur only v3.0

ggoldens commented 5 years ago

I've spent many hours on this. Using pods worked to me.

AGPatel commented 5 years ago

Yes i got it , you need to change the setting.gradle from android

from->

include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '..\node_modules\react-native-gesture-handler\android')

to ->

include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')

if you install anything from npm install... so first check the setting.gradle and it's same like backward slash( \ ) so change to forward slash ( / )

Nizzam123 commented 5 years ago

I had tried all the solution given above. But I still failed and getting this error.

This is my env : "react": "16.6.3", "react-native": "0.58.5", "react-native-gesture-handler": "^1.0.16", "react-navigation": "^3.3.2"

npm : v6.8.0

screenshot 2019-02-26 at 5 41 00 pm

Appreciate any help. Thanks.

Dhirajbhujbal commented 5 years ago

@Nizzam123 try to manual integration of the navigation lib.

Nizzam123 commented 5 years ago

@Dhirajbhujbal I did it, plus also reinit the native project and install the library, the result still same. Sigh.

Dhirajbhujbal commented 5 years ago

follow the below step

install react-native-navigation lib 3.0 1 npm install --save react-navigation 2 npm install --save react-native-gesture-handler 3 react-native link

check whether all configuration is fine or not if still getting the same issue then you might be missing so the configuration so after step 2 try to add RNGestureHandler.xcodeproj to your lib. a folder in the Xcode then add libRNGestureHandler.a file into 'Link Binary With libraries' also in the build setting put header Search path as '$(SRCROOT)/../node_modules/react-native-navigation/lib/ios'.

and clean the Xcode and try to build again hope this will work

Thanks, Dhiraj

RicardoBer commented 5 years ago

In iOS

Add this to your Podfile

pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler'

Remove "Pods" folder located into "ios" Directory

then >pods install

Finally reboot your mac, yes, I'm think that it is a joke but no, restart your mac.

This works for me.