Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't get react-device-detect to render based on device #151

Open
cameronamini opened this issue Aug 5, 2021 · 5 comments
Open

can't get react-device-detect to render based on device #151

cameronamini opened this issue Aug 5, 2021 · 5 comments

Comments

@cameronamini
Copy link

I'm trying to render my website differently based on whether it's being viewed on desktop or mobile. However, even though I'm using react-device-detect it is not working as intended. Even when viewing my website on mobile, I end up getting both the components meant to be rendered in MobileView as well as the components rendered in BrowserView.

I made a code sandbox to replicate the issue, and in the sandbox it only shows the BrowserView component even when I change the view to mobile.

Mobile Device: Android, Galaxy Note 8
Desktop: Acer Aspire 5

@andrewlorenz
Copy link

in your code sandbox your code is:

{isMobile ? <Hero /> : <MobileHero />}

so if mobile is detected render the non-mobile view otherwise render the mobile-view .... hhmmm !

@cameronamini
Copy link
Author

in your code sandbox your code is:

{isMobile ? <Hero /> : <MobileHero />}

so if mobile is detected render the non-mobile view otherwise render the mobile-view .... hhmmm !

Oh yikes, must have been playing around with it and left it like that. I fixed it but I'm still getting the the error

@andrewlorenz
Copy link

you are using isMobile - which checks the device type not the dimensions of the viewport. This package is called react-device-detect which is a very accurate name!

You can use css / media queries, or if you want to detect it in your code, I suggest you use the react-responsive package. This has a really cute hook called useMediaQuery which allows you to code media queries based on breakpoints, e.g.

import { useMediaQuery }        from 'react-responsive';

const MyComponent = () => {

  const isNarrow = useMediaQuery({ maxWidth: 600 });
  const isMedium = useMediaQuery({ minWidth: 601, maxWidth: 1200 });
  const isWide   = useMediaQuery({ minWidth: 1201 });

  // etc
}

@cameronamini
Copy link
Author

Hmm, we use it at my workplace and for our product it does seem to render differently based on screen size only. For example mobile view renders if I choose a smaller screen size in chrome inspector there.

@cameronamini
Copy link
Author

But in any case using the package you mentioned seems like a good alternative, thank you for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants