简体   繁体   中英

dynamically update device info react native

I am using react-native-device-info to get DeviceLocale or DeviceCountry. But is there a way that I can update Device-info with out restarting the app. For Example, on device settings If my language is set to "English(US)", Device-info cam detect that but if I change the settings to "English(CA)",Device-Info is not able to capture that until I restart the app.

Any help is really appreciated.

How are you changing the settings of the language?

If it's built in...you can do something like setting the language as a state of the component and once call the function to change the language settings, you can setState and it'll update the component with the new value.

If you're talking about hopping out of the app and manually changing the language settings then maybe setting creating a setInterval to check the device-info will work.

Assuming you're defining the reference to DeviceInfo outside of the component's class definition, you could try moving it inside like this.

class MyComponent extends React.Component {
  checkDeviceInfo() {
    let DeviceInfo = require('react-native-device-info');
    let locale = DeviceInfo.getDeviceLocale();
    // do something with locale
  }

  render() {
    return (
      <TouchableOpacity onPress={this.checkDeviceInfo.bind(this)}>
        <Text>Touch Here</Text>
       </TouchableOpacity>
    )
  }
}

You could put this in your component so that it's called whenever you want to check for DeviceInfo changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM