Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useResponsive): use isBrowser instead of windowExists #1682

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/hooks/src/useResponsive/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react';
import isBrowser from '../utils/isBrowser';

type Subscriber = () => void;

Expand Down Expand Up @@ -49,8 +50,9 @@ export function configResponsive(config: ResponsiveConfig) {
}

export function useResponsive() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里有问题的。

以前在没有 window 的情况下,返回的是 info,现在返回的是 undefined,很不合理。

之前的更合理。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

没有 window 的情况下, info 就是 undefined

const windowExists = typeof window !== 'undefined';
if (windowExists && !listening) {
if (!isBrowser) return;

if (!listening) {
info = {};
calculate();
window.addEventListener('resize', handleResize);
Expand All @@ -59,8 +61,6 @@ export function useResponsive() {
const [state, setState] = useState<ResponsiveInfo>(info);

useEffect(() => {
if (!windowExists) return;

const subscriber = () => {
setState(info);
};
Expand Down