使用react-native-system-setting时出现UnExpected token错误

时间:2017-09-21 13:11:57

标签: android react-native react-native-android

要在手机中启用位置,我使用了react-native-system-setting。但在使用时,我得到了意外的令牌错误。有人可以澄清我,如何使用它来反应原生代码。



// this is my error


Unexpected token (12:15)
  10 |   };
  11 | 
> 12 |   SystemSetting.isLocationEnabled().then((enable)=>{
     |                ^
  13 |     const state = enable ? 'On' : 'Off';
  14 |     console.log('Current location is ' + state);
  15 | })






// this is my CODE



import React, { Component } from "react";
import SystemSetting from 'react-native-system-setting';


export default class PhoneSetting extends Component {


  SystemSetting.isLocationEnabled().then((enable)=>{
  // got error on above line
    const state = enable ? 'On' : 'Off';
    console.log('Current location is ' + state);
})

 SystemSetting.switchLocation(()=>{
    console.log('switch location successfully');
})

  render() {
    var self = this;
    return (
      <View>
      // some content
      </View>
    );
  }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您应该将其打包在constructor()componentWillMount()

SystemSetting.switchLocation()也应仅在SystemSetting.isLocationEnabled()返回false时调用它,如果你想启用它

import React, { Component } from "react";
import SystemSetting from 'react-native-system-setting';


export default class PhoneSetting extends Component {

constructor() {
 super();
   SystemSetting.isLocationEnabled().then((enable)=>{
  // got error on above line
    const state = enable ? 'On' : 'Off';
    console.log('Current location is ' + state);
   })

  SystemSetting.switchLocation(()=>{
    console.log('switch location successfully');
  })
}
render() {
  var self = this;
  return (
   <View>
    // some content
    </View>
 );
}
}