react-ua
React User Agent Component and Provider, SSR-ready, using new React Context API
Requirement
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'));
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