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

isMobile doesn't work in Nexjs13 app #218

Open
pj-alvarado10 opened this issue Jul 31, 2023 · 3 comments
Open

isMobile doesn't work in Nexjs13 app #218

pj-alvarado10 opened this issue Jul 31, 2023 · 3 comments

Comments

@pj-alvarado10
Copy link

I use isMobile but it doesn't work when I open the website in web browser from the mobile. I need change a redirection (skype: to desktop, tel: to mobile), but the library always redirect to skype, even from a mobile device.

My code in Nextjs13 app is:

    const getCallLink = () : string => {
        if(isMobile) {
            return 'tel:';
        }
        else {
            return 'skype:';
        }
    }

I tested in a Xiami Redmi Device from Google Chrome browser.

@DLParkin
Copy link

DLParkin commented Aug 8, 2023

I had the same issue, was a little upsetting as I am porting over some react to nextjs and did not want to have to rewrite it just yet.

I ended up making a hook which seems to be working ok, it uses requestAnimationFrame before checking the isMobile which seems to give it enough time to load in and be ready for validation.

An alternative is a setTimeOut but that may be more flaky than this.

Anyway enough foo

import { useState, useEffect } from "react";
import { isMobile } from "react-device-detect";

export const useDelayedMobileDetection = (): boolean => {
  const [hideUntilValidated, setHideUntilValidated] = useState<boolean>(false);

  useEffect(() => {
    requestAnimationFrame(() => {
      if (!isMobile) {
        setHideUntilValidated(true);
      }
    });
  }, []);

  return hideUntilValidated;
};

usage
const shouldHaveLoaded = useDelayedMobileDetection();

Hope it helps or at least gives some ideas on it..

@pj-alvarado10
Copy link
Author

pj-alvarado10 commented Aug 11, 2023

Thank you!!, It works for me, too!!!

@clintonmedbery
Copy link

This worked for me:
https://stackoverflow.com/a/77174374/3058839

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

3 participants