Giter VIP home page Giter VIP logo

react-device-battery's Introduction

๐Ÿ”‹ React Device Battery

๐Ÿ‘€ In development

Notifies your React app of the device battery status

This component is based on the Battery Status API

The Battery Status API, more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change.

Demo

Edit react-battery

๐Ÿ“ฆ Installation

npm install react-device-battery

๐Ÿš€ Load

// using es modules
import Battery from 'react-device-battery'

// common.js
const Battery = require('react-device-battery')

// AMD

Or use script tags and globals.

<script src="https://unpkg.com/react-device-battery"></script>

And then grab it off the global like so:

const Battery = reactDeviceBattery

๐Ÿ’ก Usage

Let's assume you want to check if you have enough battery before doing somenthing or you want to render based on how much battery you got left:

const App = () => (
  <div>
 ย  ย <h2>Magic is happening โšก๏ธ</h2>
    <Battery 
      onChange={(battery ) => {
        console.log(battery)
      }}
      render={({ battery }) =>
        <p>Battery Level: {battery ? battery : <span>Not Supported</span>}.</p>
      } 
    />
  </div>
);

render(<App />, document.getElementById('root'));

If the Battery API is not supported, the render function is passed null.

Props

render [required]

Default: null

Whatever you'd like to render in response to changes in the battery level

<Battery 
  render={({ battery }) =>
    <p>Battery Level: {battery ? battery : <span>Not Supported</span>}.</p>
  } 
/>

onChange [optional]

Default: undefined

Called whenever the batter level changes

handleBatteryChange = ({ battery }) => {
  if (battery <= 10) {
    this.saveMyData(); // Oh my...
  }
}

<Battery 
  onChange={this.handleBatteryChange}
  render={({ battery }) =>
    <p>Battery Level: {battery ? battery : <span>Not Supported</span>}.</p>
  } 
/>

Component info

This component has been built using React Render Callback pattern. Basically it is a way for the parent to provide some logic to the child, and the child have the control on how to execute that logic.

Legal

Released under MIT license.

react-device-battery's People

Contributors

ethertyper avatar zanonnicola avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.