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

No longer run smoothly at Next.js 13 + React.js 18 (Typescript) #217

Open
mashwishi opened this issue Jul 19, 2023 · 7 comments
Open

No longer run smoothly at Next.js 13 + React.js 18 (Typescript) #217

mashwishi opened this issue Jul 19, 2023 · 7 comments

Comments

@mashwishi
Copy link

  1. Bug description
  • When trying to implement a conditional isMobile to print content it returns issues (unlike on previous setup at Nextjs 12 + React 17)
  1. Steps to reproduce
  • Create a page where there is a condition that will return a component for mobile and desktop
  1. Device user-agent
    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82

  2. Device/Browser type/name
    Windows 11 Pro 64Bit - Microsoft Edge

  3. Code
    code

  4. Issue:
    image

@mashwishi
Copy link
Author

code

Just to let you know i have also tried this method

@mashwishi
Copy link
Author

This is my temp solution:
code

@Keeper991
Copy link

I solved this problem by using useState, which has false as an initial value, and useEffect, which calls 'setState(isMobile)' at the time of componentDidMount.

I think it happens if there is a difference between the initial rendering result of client-side and the server-side rendering result.

@anoop0567
Copy link

@mashwishi have you found any better workaround for this?

@mashwishi
Copy link
Author

@mashwishi have you found any better workaround for this?

No, I posted my temp solution

@nikhil647
Copy link

nikhil647 commented Feb 4, 2024

I found answer on stack overflow. here

Make component server component.

import { getSelectorsByUserAgent } from "react-device-detect"
import { headers } from "next/headers"

const { isMobile } = getSelectorsByUserAgent(
    headers().get("user-agent") ?? ""
  )

  return (
    <>
      {(isMobile) ?
        <nav>Nabar Mobile</nav>
      :
        (
          <h1>This is rendered only on desktop</h1>  
        )
      }
    </>
  );

@naseef0
Copy link

naseef0 commented Feb 20, 2024

I found answer on stack overflow. here

Make component server component.

import { getSelectorsByUserAgent } from "react-device-detect"
import { headers } from "next/headers"

const { isMobile } = getSelectorsByUserAgent(
    headers().get("user-agent") ?? ""
  )

  return (
    <>
      {(isMobile) ?
        <nav>Nabar Mobile</nav>
      :
        (
          <h1>This is rendered only on desktop</h1>  
        )
      }
    </>
  );

@nikhil647 I have tried this but for tablet devices it's detecting as desktop.

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

5 participants