Skip to content

Commit

Permalink
feat: useMobile hook (#422)
Browse files Browse the repository at this point in the history
* feat: useMobile hook

* feat: use useLayoutEffect

* chore: update comment
  • Loading branch information
MadCcc committed Feb 20, 2023
1 parent c7d41e2 commit e59b09c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

lint-staged
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@
"es"
],
"scripts": {
"build": "dumi build",
"compile": "father build",
"coverage": "npm test -- --coverage",
"build": "dumi build",
"lint": "eslint src/ --ext .tsx,.ts & eslint tests/ --ext .js",
"prepare": "husky install",
"prepublishOnly": "npm run compile && np --yolo --no-publish",
"start": "dumi dev",
"test": "rc-test"
},
"lint-staged": {
"**/*.{js,jsx,tsx,ts,md,json}": [
"prettier --write",
"git add"
]
},
"dependencies": {
"@babel/runtime": "^7.18.3",
"react-is": "^16.12.0"
Expand All @@ -47,6 +54,8 @@
"eslint": "~7.32.0",
"father": "^4.1.3",
"glob": "^7.1.6",
"husky": "^8.0.3",
"lint-staged": "^13.1.2",
"np": "^6.2.3",
"rc-test": "^7.0.14",
"react": "^18.0.0",
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useMobile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useLayoutEffect, useState } from 'react';
import isMobile from '../isMobile';

/**
* Hook to detect if the user is on a mobile device
* Notice that this hook will only detect the device type in effect, so it will always be false in server side
*/
const useMobile = (): boolean => {
const [mobile, setMobile] = useState(false);

useLayoutEffect(() => {
setMobile(isMobile());
}, []);

return mobile;
};

export default useMobile;
27 changes: 24 additions & 3 deletions tests/hooks.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { fireEvent, render } from '@testing-library/react';
import * as React from 'react';
import { renderToString } from 'react-dom/server';
import { render, fireEvent } from '@testing-library/react';
import useId, { resetUuid } from '../src/hooks/useId';
import useLayoutEffect from '../src/hooks/useLayoutEffect';
import useMemo from '../src/hooks/useMemo';
import useMergedState from '../src/hooks/useMergedState';
import useLayoutEffect from '../src/hooks/useLayoutEffect';
import useMobile from '../src/hooks/useMobile';
import useState from '../src/hooks/useState';
import useId, { resetUuid } from '../src/hooks/useId';

global.disableUseId = false;

Expand Down Expand Up @@ -498,4 +499,24 @@ describe('hooks', () => {
global.disableUseId = false;
});
});

describe('useMobile', () => {
it('should work', () => {
const Demo = () => {
const isMobile = useMobile();
return <div>{isMobile ? 'mobile' : 'pc'}</div>;
};

const { container } = render(<Demo />);
expect(container.textContent).toBe('pc');

const navigatorSpy = jest
.spyOn(navigator, 'userAgent', 'get')
.mockImplementation(() => 'Android');
const { container: container2 } = render(<Demo />);
expect(container2.textContent).toBe('mobile');

navigatorSpy.mockRestore();
});
});
});

1 comment on commit e59b09c

@vercel
Copy link

@vercel vercel bot commented on e59b09c Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

util – ./

util-git-master-react-component.vercel.app
util.vercel.app
util-react-component.vercel.app

Please sign in to comment.