TaskQueue:任务错误:未定义不是本机反应中的对象(评估'_this.view._component.measureInWindow')

div*_*tel 16 react-native react-redux react-native-android

我是本机反应的新手,我在处理屏幕导航时使用非常简单的演示应用程序面临这个问题 收到此错误消息 TaskQueue: Error with task: undefined is not an object (evaluating '_this.view._component.measureInWindow')

这是错误的屏幕截图:

在此处输入图片说明

这是我的代码 App.js

import React, {Component} from 'react';
import {createStackNavigator} from 'react-navigation';
import HomeActivity from './components/HomeActivity';
import ProfileActivity from './components/ProfileActivity';
const RootStack = createStackNavigator(
  {
    Home: {
      screen: HomeActivity,
    },
    Profile: {
      screen: ProfileActivity,
    },
  },
  {
    initialRouteName: 'Home',
  },
);
export default class App extends Component {
  render() {
    return(
    <RootStack />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

主页Activity.js

import React, {Component} from 'react';
import {StyleSheet, Text, View, Button} from 'react-native';
class HomeActivity extends Component {
  static navigationOptions = {
    title: 'Home',
    headerStyle: {backgroundColor: '#03A9F4'},
    headerTintColor: '#fff',
    headerTitleStyle: {fontWeight: 'bold'},
  };
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.headerText}>Home Activity</Text>
        <Button
          title="Go to Profile Activity"
          onPress={() => this.props.navigation.navigate('Profile')}
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  headerText: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    fontWeight: 'bold',
  },
});
export default HomeActivity;
Run Code Online (Sandbox Code Playgroud)

ProfileActivity.js

import React, {Component} from 'react';
import { StyleSheet, Text, View, Button} from 'react-native';
class ProfileActivity extends Component {
  static navigationOptions = {
    title: 'Profile',
    headerStyle: {backgroundColor: '#73C6B6'},
  };
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.headerText}>Profile Activity</Text>
        <Button
          title="Go to Home"
          onPress={() => this.props.navigation.navigate('Home')}
        />
        <Text style={styles.headerText}>Create a New Profile Screen </Text>
        <Button
          title="Go to new Profile"
          onPress={() => this.props.navigation.push('Profile')}
        />
        <Text style={styles.headerText}> Go Back </Text>
        <Button
          title="Go Back"
          onPress={() => this.props.navigation.goBack()}
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  headerText: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    fontWeight: 'bold',
  },
});
export default ProfileActivity;
Run Code Online (Sandbox Code Playgroud)

 "dependencies": {
    "@react-native-community/masked-view": "^0.1.7",
    "@react-navigation/native": "^5.1.4",
    "react": "16.11.0",
    "react-native": "0.62.0",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-paper": "2.1.3",
    "react-native-reanimated": "^1.7.1",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-screens": "^2.4.0",
    "react-navigation": "2.6.2"
  }
Run Code Online (Sandbox Code Playgroud)

Leo*_*huk 52

这是因为 react-navigate 使用旧版本的 SafeView。

你有两种方式:

1.漫漫长路:需要迁移到v5 react-navigation v4 to v5迁移

对我来说这很困难,并且会在我的项目中进行太多更改。

2.非常快速和丑陋的解决方案:

转到目录 YOUR_PROJECT_PATH/node_modules/react-native-safe-area-view/index.js 并更新:

从:

this.view._component.measureInWindow((winX, winY, winWidth, winHeight) => {
Run Code Online (Sandbox Code Playgroud)

到:

this.view.getNode().measureInWindow((winX, winY, winWidth, winHeight) => {
Run Code Online (Sandbox Code Playgroud)

你可以试试我的叉子:

"react-navigation": "git://github.com/Snailapp/react-navigation.git#2.18.5",

更新

修复了不推荐使用的 currentFocusedField 警告:

git://github.com/Snailapp/react-navigation.git#2.18.6
Run Code Online (Sandbox Code Playgroud)

  • RN 表示不再需要 getNode()。我只是使用了 this.view.measureInWindow((winX, winY, winWidth, winHeight) =&gt; { /.. (3认同)