All Projects → quentin-sommer → React Useragent

quentin-sommer / React Useragent

Licence: mit
Integrate user-agent detection in an idiomatic React way

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Useragent

Xray React
React layout debugger.
Stars: ✭ 128 (-16.88%)
Mutual labels:  react-component
React Spinners
A collection of loading spinner components for react
Stars: ✭ 2,054 (+1233.77%)
Mutual labels:  react-component
React Vimjs
Vim in Your Web App
Stars: ✭ 149 (-3.25%)
Mutual labels:  react-component
React Sizeme
Make your React Components aware of their width and height!
Stars: ✭ 1,770 (+1049.35%)
Mutual labels:  react-component
React Share
Social media share buttons and share counts for React
Stars: ✭ 1,978 (+1184.42%)
Mutual labels:  react-component
React User Tour
Give your user a guided tour around your application
Stars: ✭ 146 (-5.19%)
Mutual labels:  react-component
Agent orange
Parse and process User Agents like a secret one
Stars: ✭ 127 (-17.53%)
Mutual labels:  user-agent
Ion Phaser
A web component to use Phaser Framework with Angular, React, Vue, etc 🎮
Stars: ✭ 152 (-1.3%)
Mutual labels:  react-component
Kea
Production Ready State Management for React
Stars: ✭ 1,805 (+1072.08%)
Mutual labels:  react-component
React Plaid Link
React bindings for Plaid Link
Stars: ✭ 148 (-3.9%)
Mutual labels:  react-component
Antd Schema Form
Based on Ant Design, interactive forms can be generated through JSON Schema configuration. - 基于Ant Design,可以通过JSON Schema配置生成可交互的表单。
Stars: ✭ 137 (-11.04%)
Mutual labels:  react-component
Create React Component Folder
Creates react component folder structure
Stars: ✭ 139 (-9.74%)
Mutual labels:  react-component
React Border Wrapper
A wrapper for placing elements along div borders.
Stars: ✭ 147 (-4.55%)
Mutual labels:  react-component
React Responsive Carousel
React.js Responsive Carousel (with Swipe)
Stars: ✭ 1,962 (+1174.03%)
Mutual labels:  react-component
React Items Carousel
Items Carousel Built with react-motion and styled-components
Stars: ✭ 150 (-2.6%)
Mutual labels:  react-component
React Native Keyboard Accessory View
Keyboard accessory (sticky) view for your React Native app. Supports interactive dismiss on iOS.
Stars: ✭ 128 (-16.88%)
Mutual labels:  react-component
Devicedetector.net
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Stars: ✭ 144 (-6.49%)
Mutual labels:  user-agent
React Ds
🔥 React Drag To Select component (tiny, touch friendly, and no dependencies!)
Stars: ✭ 153 (-0.65%)
Mutual labels:  react-component
React Layer Stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 152 (-1.3%)
Mutual labels:  react-component
React Page Progress
🎉 Sweet Component powered by React Hooks 🚀 👉
Stars: ✭ 148 (-3.9%)
Mutual labels:  react-component

react-useragent

Integrate user-agent detection in an idiomatic React way.

Installation

yarn add @quentin-sommer/react-useragent or npm i -s @quentin-sommer/react-useragent

For React 15 (old context) use the 2.x version

// React 15
"dependencies": {
  ...
  "@quentin-sommer/react-useragent": "^2.0.0"
  ...
}

Introduction

Imagine being able to render magnificent, deep links, beautiful download buttons for your app. Well, Now you can.

<div>
  <UserAgent ios>
    <BeautifulIOSButton />
  </UserAgent>
  <UserAgent windows>
    <BeautifulWindowsButton />
  </UserAgent>
</div>

react-useragent wraps the great UAParser.js library and make it easy to use useragent knowledge inside your React applications. react-useragent provides useful shortcuts but you can always use an escape hatch in case you want to access the underlying library.

live demo

Usage

Next.js example

The most common question about this library is how to use it with Next.js. An example is available in an issue.

Generic usage

First you need to wrap your App in a <UserAgentProvider> tag. You also need to pass a user agent string to <UserAgentProvider>. On the browser that would be window.navigator.userAgent.

react-useragent works in server side rendering as well, just pass it the request useragent string. On express that would be req.headers['user-agent'].

import {UserAgentProvider} from '@quentin-sommer/react-useragent'

const App = props => (
  <UserAgentProvider ua={window.navigator.userAgent}>
    <div>{/* rest of your App */}</div>
  </UserAgentProvider>
)

Then use the <UserAgent> component.

react-useragent expose some props, these are optimized and using them will be faster than directly accessing the UAParser.js library.

Available props for <UserAgent>

  • computer
  • windows
  • linux
  • mac
  • mobile
  • tablet
  • android
  • ios
  • firefox
  • chrome
  • edge
  • safari

Theses props are cumulable : <UserAgent firefox mobile> will match both firefox browser and mobile systems.

import {UserAgentProvider, UserAgent} from '@quentin-sommer/react-useragent'

const App = props => (
  <UserAgentProvider ua={window.navigator.userAgent}>
    <div>
      <UserAgent mobile>
        <p>This will only be rendered on mobile</p>
      </UserAgent>
    </div>
  </UserAgentProvider>
)

You can also use this alternative API if you find it more convenient

<UserAgent mobile>
    {uaIsMobile => (
        {uaIsMobile && <p>This will ONLY be rendered on mobile</p>}
        {!uaIsMobile && <p>This will NOT be rendered on mobile</p>}
    )}
</UserAgent>

For full power you can always access the underlying parser with the returnFullParser prop

<UserAgent returnFullParser>
  {parser => (
    <p>
      I see you, {parser.getOS().name} {parser.getCPU().architecture}
    </p>
  )}
</UserAgent>

You can also use the library with the useContext hook

import {UAContext} from '@quentin-sommer/react-useragent'
const UsingContextHook = () => {
  const {uaResults, parser} = useContext(UAContext)
  return parser.getOS().name
}

For more example see the demo app source here

If you have any questions don't hesitate to say hi on Twitter

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].