问题
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