Skip to content

📱React User Agent Component, Hook, and HOC. SSR-ready, full UT, using new React Context and Hooks API

License

Notifications You must be signed in to change notification settings

antonybudianto/react-ua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-ua

npm version Build Status

React User Agent Component and Provider, SSR-ready, using new React Context API

Requirement

  • React 16.8.0

Features

Try it live at StackBlitz

import React from 'react';
import { UserAgentProvider, useUserAgent } from 'react-ua';

const Comp = () => {
  const ua = useUserAgent();
  return <div>OS: {ua.os.name}</div>;
};

const App = () => (
  <UserAgentProvider>
    <Comp />
  </UserAgentProvider>
);

ReactDOM.render(<App />, document.getElementById('#root'));

// SSR
const el = (
  <UserAgentProvider value={request.headers['user-agent']}>
    <Comp />
  </UserAgentProvider>
);

ReactDOMServer.renderToString(el);

HOC (deprecated)

import { withUserAgent } from 'react-ua/hoc';

const CompWithHoc = withUserAgent(({ ua }) => <div>OS: {ua.os.name}</div>);

const App = () => (
  <UserAgentProvider>
    <CompWithHoc />
  </UserAgentProvider>
);

License

MIT

About

📱React User Agent Component, Hook, and HOC. SSR-ready, full UT, using new React Context and Hooks API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published