Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  195] [ 2]  / answers: 1 / hits: 5999  / 4 Years ago, sat, october 24, 2020, 12:00:00

I'm writing a small function in Next JS with react. When I use navigator, I get the following error.


ReferenceError: navigator is not defined


Here's my code:


import React from "react";

export default function App() {

const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;

return (
{isMac ? "I'm mac" : "I'm windows"}
);
}

How do I get browser platform and then render a section based on that?


More From » reactjs

 Answers
13

Essentially, this is what I need to write.


const isMac = typeof window !== 'undefined' ? navigator.platform.toUpperCase().indexOf("MAC") >= 0 : false;

[#2428] Monday, October 19, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;