React hook for magnetometer sensor access
npm install react-hook-magnetometerA React hook to access data from the Magnetometer API.
Using npm:
``sh`
npm install --save react-hook-magnetometer
Using yarn:
`sh`
yarn add react-hook-magnetometer
`jsx
import React from 'react'
import useMagnetometer from 'react-hook-magnetometer'
const MyComponent = () => {
const magnetometer = useMagnetometer()
return !magnetometer.error ? (
No magnetometer, sorry.
$3
If you want to use this feature, simply provide
useMagnetometer with a SensorOptions object:`jsx
const magnetometer = useMagnetometer({
frequency: 60, // cycles per second
})
``Access to data from the Magnetometer API needs user permission.
If permission to access magnetometer was previously granted by the user, magnetometer data will be available. If permission to access was not granted previously, the user will be prompted to give permission when the component mounts.
Magnetometer 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