Skip to content

Commit 121c4b5

Browse files
authored
fix: useMobile should not warn useLayoutEffect in SSR (#435)
1 parent 2410781 commit 121c4b5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/hooks/useMobile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useLayoutEffect, useState } from 'react';
1+
import { useState } from 'react';
22
import isMobile from '../isMobile';
3+
import useLayoutEffect from './useLayoutEffect';
34

45
/**
56
* Hook to detect if the user is on a mobile device

tests/hooks.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,5 +518,18 @@ describe('hooks', () => {
518518

519519
navigatorSpy.mockRestore();
520520
});
521+
522+
it('should not warn useLayoutEffect in SSR', () => {
523+
const errorSpy = jest.spyOn(console, 'error');
524+
const Demo = () => {
525+
useMobile();
526+
return null;
527+
};
528+
renderToString(<Demo />);
529+
expect(errorSpy).not.toHaveBeenCalledWith(
530+
expect.stringContaining('useLayoutEffect'),
531+
expect.anything(),
532+
);
533+
});
521534
});
522535
});

0 commit comments

Comments
 (0)