简体   繁体   English

React Native:使用内置按钮时出错

[英]React Native: Error while using built-in button

I am trying to learn React Native and while the sample app works, I get an error, on using the Button component. 我正在尝试学习React Native,并且在示例应用程序运行时,在使用Button组件时出现错误。 The error comes in the normal red background on my Android Lollipop 5.1 smartphone. 该错误来自我的Android Lollipop 5.1智能手机的正常红色背景。

java.lang.String cannot be cast to
com.facebook.react.uimanager.AccessiblityDelegate$AccessibilityRole

setDelegate
AccessbilityDelegateUtil.java:93

updateViewAccessibility
BaseViewManager.java:260

// and more stack trace was given

App.js contains the following code which does not show any errors in VS Code. App.js包含以下代码,该代码在VS代码中未显示任何错误。

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'This was edited by Shukant Pal for testing',
});

type Props = {};
export default class App extends Component<Props> {
_startGame = () => {

}

_loadExistingGame = () => {

}

_loadTemplateLibrary = () => {

}

_displayAboutUs = () => {

}

render() {
  return (
    <View style={styles.container}>
      <Text>This works</Text>
      <View style={styles.container}>
        <Button onPress={this._startGame} title="New Game" />
      </View>
    </View>
  );
}
}

const styles = StyleSheet.create({
 container: {
   flex: 1,
   justifyContent: 'center',
   alignItems: 'center',
   backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

I did the following checks before posting this question: 发布此问题之前,我做了以下检查:

  1. Imported Button from react-native 从本机导入的按钮

  2. Before onPress={this._startGame} was giving errors. onPress={this._startGame}出现错误之前。 After changing the method declarations from name() to name = () => {} that was resolved. 将方法声明从name()更改为name = () => {} ,即可解决。 Could anyone explain why? 谁能解释为什么? (I am new to React and React Native) (我是React和React Native的新手)

Because of the way the javascript "this" keyword works. 由于javascript“ this”关键字的工作方式。 When using "this" on an event listener the "this" keyword does not refer to the class component anymore it refers to the element which the event was acted upon so that's why you are getting an error. 在事件侦听器上使用“ this”时,“ this”关键字不再引用类组件,而是引用事件所作用的元素,因此这就是您得到错误的原因。

Check this article out it gives a detailed explanation of the this keyword. 查看本文,它提供了此关键字的详细说明。

this - Javascript | 这个-Javascript | MDN MDN

You can bind your function if you want to use it without arrow function. 如果要使用不带箭头功能的功能,则可以绑定功能。

For example. 例如。

constructor(props) {
  super(props);

  // This binding is necessary to make `this` work in the callback
  this._startGame = this._startGame.bind(this);
}

_startGame() {
  // Do something
}

Why ? 为什么呢

You have to be careful about the meaning of this in JSX callbacks. 您必须小心JSX回调中的含义。 In JavaScript, class methods are not bound by default. 在JavaScript中,默认情况下不绑定类方法。 If you forget to bind this.handleClick and pass it to onClick, this will be undefined when the function is actually called. 如果忘记绑定this.handleClick并将其传递给onClick,则在实际调用该函数时将无法定义。

This is not React-specific behavior; 这不是特定于React的行为; it is a part of how functions work in JavaScript. 它是函数在JavaScript中工作方式的一部分。 Generally, if you refer to a method without () after it, such as onClick={this.handleClick}, you should bind that method. 通常,如果引用的方法后面没有(),例如onClick = {this.handleClick},则应绑定该方法。

Official 官方

Your way is class properties in ES7 stage 2 as _startGame = () => {} . 您的方式是ES7阶段2中的类属性,如_startGame = () => {}

https://borgs.cybrilla.com/tils/es7-class-properties/ https://borgs.cybrilla.com/tils/es7-class-properties/

And CRNA is including babel-plugin-proposal-class-properties for this syntax. CRNA为此语法包括babel-plugin-proposal-class-properties That is how we can use the syntax without additional settings on babel. 这样便可以在不对babel进行其他设置的情况下使用语法。

https://babeljs.io/docs/en/babel-plugin-proposal-class-properties https://babeljs.io/docs/zh-CN/babel-plugin-proposal-class-properties

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Expo React Native 错误:元素类型无效:应为字符串(对于内置组件) - Expo React Native Error: Element type is invalid: Expected a string (for built-in components) React Native,元素类型无效:需要一个字符串(对于内置组件) - React Native, element type is invalid: expected a string (for built-in components) 在 React Native App 中,我得到了错误:元素类型无效:期望一个字符串(对于内置组件)...检查 `App` 的渲染方法? - In React Native App, I Got the Error: Element type is invalid: expected a string (for built-in components) ... Check the render method of `App`? 使用内置验证来验证在Magento中单击/选择的按钮 - Validate for a button click/selected in Magento using the built-in validation 如何在 React Native 项目中使用 Node.JS 的 Crypto 内置模块? - How can I use Crypto built-in module from Node.JS in React Native Project? 使用send内置操作 - Using send built-in action 将 Polyline 与 react-native 地图一起使用时出现空引用错误 - Null reference error while using Polyline with react-native maps 使用 react-native-draggable-flatlist 时在 react native 中出现未定义的错误 - Getting an undefined error in react native while using react-native-draggable-flatlist React maps 错误预期是字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - React maps Error expected a string (for built-in components) or a class/function (for composite components) but got: undefined React Big Calendar错误:元素类型无效:预期为字符串(对于内置组件) - React Big Calendar Error: Element type is invalid: expected a string (for built-in components)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM