React Native 在配置react-navigation时会出现这个问题,在模拟器和真机上面都会出现了以下错误,后来发现react-navigation官网上解决方法,在此记录:
Cannot read property 'Direction' of undefined
解决方法如下:
1 、react-navigation
在React Native项目中安装包。
yarn add react-navigation //下载可以按照需要版本下载 yarn add react-Navigation@3.12.1
# or with npm
# npm install --save react-navigation
React Navigation由一些核心实用程序组成,然后导航器使用这些实用程序在您的应用程序中创建导航结构。现在不要太担心这个,它很快就会变得清晰!要预先安装工作,我们还要安装和配置大多数导航器使用的依赖项,然后我们可以继续开始编写一些代码。
我们现在要安装的库是react-native-gesture-handler
和react-native-reanimated
。如果您已经安装了这些库并且安装了最新版本,那么您就完成了!否则,请继续阅读。
将依赖项安装到Expo托管项目中
在项目目录中,运行expo install react-native-gesture-handler react-native-reanimated
。这将安装兼容的这些库的版本
您现在可以继续“Hello React Navigation”开始编写一些代码。
将依赖项安装到一个简单的React Native项目中
在项目目录中,运行yarn add react-native-reanimated react-native-gesture-handler react-native-screens
。
接下来,我们需要链接这些库。步骤取决于您的React Native版本:
cd ios
pod install
cd ..
-
React Native 0.59及更低版本
如果您使用较旧的React Native版本,则需要手动链接依赖项。为此,请运行:
react-native link react-native-reanimated react-native link react-native-gesture-handler react-native link react-native-screens
要完成安装react-native-screens
的Android,下面两行添加到dependencies
节中android/app/build.gradle
:
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'
要完成react-native-gesture-handler
Android的安装,请进行以下修改MainActivity.java
:
package com.reactnavigation.example;
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 {
@Override
protected String getMainComponentName() {
return "Example";
}
+ @Override
+ protected ReactActivityDelegate createReactActivityDelegate() {
+ return new ReactActivityDelegate(this, getMainComponentName()) {
+ @Override
+ protected ReactRootView createRootView() {
+ return new RNGestureHandlerEnabledRootView(MainActivity.this);
+ }
+ };
+ }
}
本文来自:react-navigation官网
网友评论