A React hook to access data from the Gravity Sensor API.
Using npm
:
npm install --save react-hook-gravity
Using yarn
:
yarn add react-hook-gravity
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>
)
}
If you want to use this feature, simply provide useGravitySensor
with a SensorOptions
object:
const sensor = useGravitySensor({
frequency: 60, // cycles per second
})
Access to data from the Gravity Light API needs user permission.
Gravity Light API is available only in secure contexts (only using HTTPS).
Credit to Bence A. Tóth for his original hook code for Geolocation.
LGPL-3.0