use-platform-os hook optimization to not cause re renders (#5453)
This commit is contained in:
parent
4598b1b49d
commit
c1d3da0cab
2 changed files with 15 additions and 33 deletions
|
|
@ -1,15 +1,6 @@
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
|
|
||||||
export const usePlatformOS = () => {
|
export const usePlatformOS = () => {
|
||||||
// states
|
const userAgent = window.navigator.userAgent;
|
||||||
const [isMobile, setIsMobile] = useState(false);
|
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const userAgent = window.navigator.userAgent;
|
|
||||||
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);
|
|
||||||
|
|
||||||
if (isMobile) setIsMobile(isMobile);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return { isMobile };
|
return { isMobile };
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,20 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
|
|
||||||
export const usePlatformOS = () => {
|
export const usePlatformOS = () => {
|
||||||
const [isMobile, setIsMobile] = useState(false);
|
const userAgent = window.navigator.userAgent;
|
||||||
const [platform, setPlatform] = useState("");
|
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);
|
||||||
useEffect(() => {
|
let platform = "";
|
||||||
const userAgent = window.navigator.userAgent;
|
|
||||||
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);
|
|
||||||
let detectedPlatform = "";
|
|
||||||
|
|
||||||
if (isMobile) {
|
if (!isMobile) {
|
||||||
setIsMobile(isMobile)
|
if (userAgent.indexOf("Win") !== -1) {
|
||||||
|
platform = "Windows";
|
||||||
|
} else if (userAgent.indexOf("Mac") !== -1) {
|
||||||
|
platform = "MacOS";
|
||||||
|
} else if (userAgent.indexOf("Linux") !== -1) {
|
||||||
|
platform = "Linux";
|
||||||
} else {
|
} else {
|
||||||
if (userAgent.indexOf("Win") !== -1) {
|
platform = "Unknown";
|
||||||
detectedPlatform = "Windows";
|
}
|
||||||
} else if (userAgent.indexOf("Mac") !== -1) {
|
}
|
||||||
detectedPlatform = "MacOS";
|
|
||||||
} else if (userAgent.indexOf("Linux") !== -1) {
|
|
||||||
detectedPlatform = "Linux";
|
|
||||||
} else {
|
|
||||||
detectedPlatform = "Unknown";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
setPlatform(detectedPlatform);
|
|
||||||
}, []);
|
|
||||||
return { isMobile, platform };
|
return { isMobile, platform };
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue