Git Product home page Git Product logo

Comments (14)

BjoernRave avatar BjoernRave commented on June 3, 2024 1

yea, okay. Thanks for this library anyway. I will close the issue :)

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

Hi,

Could you try to explain exactly what do you want to achieve and why you would need a different api? Maybe I will be able to help you using the current API or brainstorm potential improvements!

Best

from react-useragent.

BjoernRave avatar BjoernRave commented on June 3, 2024

yea, of course. So I am using this library on the server side to detect which nav bar i need to render. the easiest way for me would be to just have a

isMobile ? <MobileNav/> : <StandardNav/>

right now I use it like this:

<Useragent mobile>
<MobileNav/>
</UserAgent>
<Useragent computer>
<StandardNav/>
</Useragent>

First up I am not sure if this covers all cases (if it falls back to computer) or not, second of all I would still like to display the mobilenav, when the user agen can not be detected, but I am on the client side and can do it based on the devices width

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

I see, would the function as a child api work for you?

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

from react-useragent.

BjoernRave avatar BjoernRave commented on June 3, 2024

afaik that is already possible, right? The cleanest solution would be to just import a boolean, but I guess I can work with that

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

Yep, this already exists. The thing is the value lives inside the React context: it's only created after the UserAgentProvider component exists. If you were to call a method / read a boolean directly from the module like this:

import {isMobile} from 'UserAgentProvider'

You would have no guarantee that it's initalized.

If you really want to bail out of React's abstraction you could use the ua-parser-js yourself and initialize it by hand at the start of your app:

// uaParser.js

let uaParser

export function init(uaString) {
 uaParser = new UAParser(uaString)
}

export function isMobile() {
  uaParser.getDevice().type === 'mobile'
}

But then you would defeat all the purpose of React encapsulation model

from react-useragent.

BjoernRave avatar BjoernRave commented on June 3, 2024

I just found this library: https://github.com/antonybudianto/react-ua. They solve it using a HOC

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

I don't see any difference between the 2 libraries 🤔

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

is roughly the same as

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

from react-useragent.

BjoernRave avatar BjoernRave commented on June 3, 2024

for me the above is a bit cleaner, as I just have the boolean. I can also use it outside of the JSX

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

I still don't see it sorry, this 2 examples are identical:

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

You will only have access to the ua object when the components get rendered by React in both cases

from react-useragent.

BjoernRave avatar BjoernRave commented on June 3, 2024

in the above example it's pretty much the same I agree, but what about:

const CompWithHoc = withUserAgent(({ ua }) => {

const someFunc = () => (
ua.device.type === 'mobile ? 'one thing' : 'the other'
)

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

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

someFunc is only usable inside it's closure, still not difference with just doing logic on ua directly for me... I think you're misunderstanding the hoc pattern, the callback is only called when the component is rendered, not before

from react-useragent.

BjoernRave avatar BjoernRave commented on June 3, 2024

for me it is, because for example this case:

const Component ({ ua }) => {

const handleSubmit = () => {
sendPOSTRequest('http:/wwww...', {name: 'xxx', fromMobile: ua.device.type === 'mobile'})
}

return (
	<form onSubmit={handleSubmit}>
		<input />
	</form>
	)
}

this is also possible with render props, but it's not as clean

from react-useragent.

quentin-sommer avatar quentin-sommer commented on June 3, 2024

You can achieve exactly this with 2 more lines using this library:

const Component = () => (
  <UserAgent returnFullParser>
    {ua => {
      const handleSubmit = () => {
        sendPOSTRequest('http:/wwww...', {
          name: 'xxx',
          fromMobile: ua.device.type === 'mobile',
        })
      }

      return (
        <form onSubmit={handleSubmit}>
          <input />
        </form>
      )
    }}
  </UserAgent>
)

Sorry but I don't want complexify the API just to save 2 lines.
Let me know if I you have any other questions

from react-useragent.

Related Issues (17)

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.