Skip to content

masaok/react-hook-gravity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A React hook to access data from the Gravity Sensor API.

Installation

Using npm:

npm install --save react-hook-gravity

Using yarn:

yarn add react-hook-gravity

Usage

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published