Detect device, and render view according to detected device type.
Installations
npm install react-device-detect
Score
90.5
Supply Chain
93.9
Quality
75.9
Maintenance
100
Vulnerability
100
License
Developer
duskload
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
16.18.0
NPM Version
8.19.2
Statistics
2,817 Stars
187 Commits
154 Forks
14 Watching
5 Branches
28 Contributors
Updated on 26 Nov 2024
Bundle Size
32.37 kB
Minified
11.89 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
133,355,898
Last day
0.4%
196,533
Compared to previous day
Last week
3.6%
987,704
Compared to previous week
Last month
9.6%
4,143,590
Compared to previous month
Last year
27.6%
44,647,298
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-device-detect
Detect device, and render view according to the detected device type.
Installation
To install, you can use npm or yarn:
npm install react-device-detect --save
or
yarn add react-device-detect
When to use this library
This library uses a technique called user agent sniffing to detect device information. That means it works by examining the User Agent string given by a browser and comparing it to a list of browser and device names it knows about. This technique works, but has drawbacks and may or may not be the right approach, depending on what you're trying to achieve. If you need to detect a specific browser type (e.g. Chrome, Safari, Internet Explorer) or specific category of device (e.g. all iPods), this library can do that. If you just want your React app to behave differently or look different on mobiles in general, CSS @media
queries and matchMedia
are probably what you want. There are many libraries that can help with using @media
queries and matchMedia
in React projects, such as react-responsive and @react-hook/media-query.
API
Usage
Example:
1import { BrowserView, MobileView, isBrowser, isMobile } from 'react-device-detect';
1<BrowserView> 2 <h1>This is rendered only in browser</h1> 3</BrowserView> 4<MobileView> 5 <h1>This is rendered only on mobile</h1> 6</MobileView>
if you don't need a view, you can use isMobile
for conditional rendering
1import {isMobile} from 'react-device-detect'; 2 3function App() { 4 renderContent = () => { 5 if (isMobile) { 6 return <div> This content is available only on mobile</div> 7 } 8 return <div> ...content </div> 9 } 10 11 render() { 12 return this.renderContent(); 13 } 14}
If you want to leave a message to a specific browser (e.g IE), you can use isIE
selector
1import { isIE } from 'react-device-detect'; 2 3function App() { 4 render() { 5 if (isIE) return <div> IE is not supported. Download Chrome/Opera/Firefox </div> 6 return ( 7 <div>...content</div> 8 ) 9 } 10}
If you want to render a view on a specific device and with a specific condition:
1import { browserName, CustomView } from 'react-device-detect'; 2 3function App() { 4 render() { 5 return ( 6 <CustomView condition={browserName === "Chrome"}> 7 <div>...content</div> 8 </CustomView> 9 ) 10 } 11}
Style the view
You can style a view component by passing class to the className
prop
1<BrowserView className="custom-class"> 2 <p>View content</p> 3</BrowserView>
or you can pass inline styles to style
prop
1const styles = { 2 background: 'red', 3 fontSize: '24px', 4 lineHeight: '2', 5};
1<BrowserView style={styles}> 2 <p>View content</p> 3</BrowserView>
Testing
1import * as rdd from 'react-device-detect'; 2 3rdd.isMobile = true; 4 5// use in tests
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 8/22 approved changesets -- score normalized to 3
Reason
9 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 21 are checked with a SAST tool
Score
2.6
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-device-detect
detect-it
Detect if a device is mouse only, touch only, or hybrid
detect-port
Node.js implementation of port detector
device-detector-js
A javascript port of Matomo device-detector
@react-native-community/cli-platform-android
This package is part of the [React Native CLI](../../README.md). It contains commands for managing the Android part of React Native app.