Created
February 29, 2016 14:13
-
-
Save klipach/2e4f37df6bc037d2c584 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
module.exports = React.createClass({ | |
getInitialState: function () { | |
return { | |
battery: 0 | |
}; | |
}, | |
componentDidMount: function () { | |
navigator.getBattery().then( | |
function (battery) { | |
this.setState({battery: battery}); | |
battery.addEventListener('chargingchange', function () { | |
this.setState({battery: battery}); | |
}.bind(this)); | |
battery.addEventListener('levelchange', function () { | |
this.setState({battery: battery}); | |
}.bind(this)); | |
}.bind(this) | |
) | |
}, | |
render: function () { | |
return (<div className="battery"> | |
<div className={ this.state.battery.charging ? 'green' : 'red' }>{ Math.floor(this.state.battery.level * 100) }%</div> | |
</div>) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment