NPM
$ npm install device-type-detector --save
Yarn
$ yarn add device-type-detector
import React, { useEffect, useState } from 'react';
import { getDeviceType } from 'device-detector';
const App = () => {
const [deviceType, setDeviceType] = useState('');
useEffect(() => {
const type = getDeviceType();
setDeviceType(type);
}, []);
return (
<div>
<h1>Device Detector</h1>
<p>Your device type is: {deviceType}</p>
</div>
);
};
export default App;
const deviceType = getDeviceType();
console.log(deviceType); // Outputs: 'mobile', 'tablet', 'desktop', or 'unknown'