Skip to content

Commit

Permalink
fix: useMobile should not warn useLayoutEffect in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc committed Mar 29, 2023
1 parent 2410781 commit a43a24c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/hooks/useMobile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useLayoutEffect, useState } from 'react';
import { useState } from 'react';
import isMobile from '../isMobile';
import useLayoutEffect from './useLayoutEffect';

/**
* Hook to detect if the user is on a mobile device
Expand Down
13 changes: 13 additions & 0 deletions tests/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,18 @@ describe('hooks', () => {

navigatorSpy.mockRestore();
});

it('should not warn useLayoutEffect in SSR', () => {
const errorSpy = jest.spyOn(console, 'error');
const Demo = () => {
useMobile();
return null;
};
renderToString(<Demo />);
expect(errorSpy).not.toHaveBeenCalledWith(
expect.stringContaining('useLayoutEffect'),
expect.anything(),
);
});
});
});

0 comments on commit a43a24c

Please sign in to comment.