Skip to content

QueenieCplusplus/React_batteryApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React_batteryApp

CodeFile

import * as React from 'react';
// expo install expo-battery
import * as Battery from 'expo-battery';
import {Text, View } from 'react-native';

export default class App extends React.Component {
  state = {
    batteryLevel: null,
  };

  componentDidMount() {
    this.subscribe();
  }

  componentWillUnmount() {
    this.unsubscribe();
  }

  async subscribe() {

    const batteryLevel = await Battery.getBatteryLevelAsync();
    this.setState({ batteryLevel });

    this.subscription = Battery.addBatteryLevelListener(({ batteryLevel }) => {
    
      this.setState({ batteryLevel });
      
      //console.log('batteryLevel changed!', batteryLevel);
      
    });

  }

  unsubscribe() {

    this.subscription && this.subscription.remove();
    this.subscription = null;

  }

  render() {
    return (
      <View>
        <Text>Current Battery Level: {this.state.batteryLevel}</Text>
      </View>
    );
  }
}

Releases

No releases published

Packages

No packages published