Skip to content

Commit

Permalink
feat(react-essentials): add useUserAgent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Jun 14, 2024
1 parent 6e87a68 commit 78a85e3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-ducks-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/react-essentials": minor
---

The useUserAgent hook has been added.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/react-essentials/hooks/useUserAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Bowser from "bowser";
import { useEffect, useState } from "react";

type Result = Bowser.Parser.ParsedResult;

let globalResult: Result;

export function useUserAgent(): Result | undefined {
const [result, setResult] = useState<Result>();

useEffect(() => {
if (!globalResult) {
globalResult = Bowser.parse(window.navigator.userAgent);
}

setResult(globalResult);
}, []);

return result;
}
5 changes: 3 additions & 2 deletions packages/react-essentials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
"react": ">=16.8.0"
},
"dependencies": {
"@codedazur/essentials": "*"
"@codedazur/essentials": "*",
"bowser": "^2.11.0"
},
"devDependencies": {
"@codedazur/eslint-config": "*",
"@codedazur/tsconfig": "*",
"@testing-library/dom": "9.3.3",
"@testing-library/react": "alpha",
"@types/react-dom": "^18.3.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"eslint": "^8.46.0",
"jsdom": "^22.1.0",
"react": "^18.3.1",
Expand Down

0 comments on commit 78a85e3

Please sign in to comment.