Giter Site home page Giter Site logo

antonybudianto / react-ua Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 3.0 372 KB

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

License: MIT License

TypeScript 100.00%
react reactjs user-agent user-agent-parser ssr hooks

react-ua's Introduction

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

react-ua's People

Contributors

1nd avatar antonybudianto avatar eduardorengifo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-ua's Issues

Using with getServerSideProps

When using getServerSideProps throws "You can not use getInitialProps with getServerSideProps"

I recently switched a few pages to getServerSideProps, but only one has withUserAgent. I'd like to use getServerSideProps on all of them, but it's currently impossible with this setup.

To Reproduce

Steps to reproduce the behavior:

  1. Switch to getServerSideProps
  2. Wrap with withUserAgent
  3. Try to render a page

Expected behavior

It should detect if you're using getServerSideProps

Could not find a declaration file for module 'react-ua'

Hi.

I have tried using react-ua and get this error

Could not find a declaration file for module 'react-ua'. '/path/to/node_modules/react-ua/dist/react-ua.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/react-ua` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-ua';`ts(7016)

running npm i --save-dev @types/react-ua did not help.

Any way of get the library working?

My environment:

    "react": "^18.2.0",
    "react-ua": "^4.0.0",

Thanks for advice or a fix

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.