1.1.2 • Published 4 years ago
react-hook-gravity v1.1.2
A React hook to access data from the Gravity Sensor API.
Installation
Using npm:
npm install --save react-hook-gravityUsing yarn:
yarn add react-hook-gravityUsage
import React from 'react'
import useGravitySensor from 'react-hook-gravity'
const ComponentWithAccelerometer = () => {
const sensor = useGravitySensor()
return !sensor.error ? (
<ul>
<li>X: {sensor.x}</li>
<li>Y: {sensor.y}</li>
<li>Z: {sensor.z}</li>
</ul>
) : (
<p>No gravity, sorry.</p>
)
}Using SensorOptions
If you want to use this feature, simply provide useGravitySensor with a SensorOptions object:
const sensor = useGravitySensor({
frequency: 60, // cycles per second
})Notes
Access to data from the Gravity Light API needs user permission.
Caveats
Gravity Light API is available only in secure contexts (only using HTTPS).
Credits
Credit to Bence A. Tóth for his original hook code for Geolocation.
License
LGPL-3.0