5
GitHub - duskload/react-device-detect: Detect device, and render view according...
source link: https://github.com/duskload/react-device-detect
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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
Usage
Example:
import { BrowserView, MobileView, isBrowser, isMobile } from 'react-device-detect';
<BrowserView> <h1>This is rendered only in browser</h1> </BrowserView> <MobileView> <h1>This is rendered only on mobile</h1> </MobileView>
if you don't need a view, you can use isMobile
for conditional rendering
import {isMobile} from 'react-device-detect'; function App() { renderContent = () => { if (isMobile) { return <div> This content is unavailable on mobile</div> } return <div> ...content </div> } render() { return this.renderContent(); } }
If you want to leave a message to a specific browser (e.g IE), you can use isIE
selector
import { isIE } from 'react-device-detect'; function App() { render() { if (isIE) return <div> IE is not supported. Download Chrome/Opera/Firefox </div> return ( <div>...content</div> ) } }
If you want to render a view on a specific device and with a specific condition:
import { browserName, CustomView } from 'react-device-detect'; function App() { render() { return ( <CustomView condition={browserName === "Chrome"}> <div>...content</div> </CustomView> ) } }
Style the view
You can style view component by passing class to viewClassName
prop
<BrowserView className="custom-class"> <p>View content</p> </BrowserView>
or you can pass inline styles to style
prop
const styles = { background: 'red', fontSize: '24px', lineHeight: '2', };
<BrowserView style={styles}> <p>View content</p> </BrowserView>
Testing
import * as rdd from 'react-device-detect'; rdd.isMobile = true; // use in tests
License
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK