Got UnExpected token error while using react-native-system-setting

穿精又带淫゛_ 提交于 2019-12-25 11:53:28

问题


For enabling location in Phone i used react-native-system-setting . But while using that i got unexpected token error . can some one clarify me , how to use this in react native codes .

// 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>
    );
  }
}

回答1:


you should wrap it in constructor() or componentWillMount()

also SystemSetting.switchLocation() should be only call it when the SystemSetting.isLocationEnabled() return false if you want to enable it

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>
 );
}
}


来源:https://stackoverflow.com/questions/46344869/got-unexpected-token-error-while-using-react-native-system-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!