首页 > 解决方案 > 意外的令牌,在 React Native 中应为“=>”

问题描述

我是 React Native 的新手,所以不熟悉箭头函数的用法。我正在尝试使用此代码来获得 wifi 许可。

我的代码

import { PermissionsAndroid } from "react-native";

async componentDidMount() {
  await this.askForUserPermission();
}

async askForUserPermission()
{
  try{
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: "Wifi Networks",
        message: "We need your permission to access wifi networks"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED){
      console.log("Thank you for your permission! :)");
    }
    else{
      console.log("You will not be able to retrieve wifi networks list");
    }
  }
  catch (err)
  {
    console.warn(err);
  }
}

请帮帮我。

标签: react-nativereact-native-androidarrow-functions

解决方案


它期望 componentDidMount() 在类中。与此类似的东西会起作用。

class someComponent extends React.Component {
  componentDidMount() {
    return this.askForUserPermission();
  }

  async askForUserPermission() {
    // Other code abbreviated.
  }
};

export default someComponent;

推荐阅读