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(useDocumentVisibility): default visibility to true for SSR #857

Closed
wants to merge 1 commit into from

Conversation

turkyden
Copy link
Contributor

@turkyden turkyden commented Feb 4, 2021

Default document visibility to true for SSR. #854

import { useState } from 'react';
import useEventListener from '../useEventListener';

type VisibilityState = 'hidden' | 'visible' | 'prerender' | undefined;

const getVisibility = () => {
- if (typeof document === 'undefined') return;
+ if (typeof document === 'undefined') return true;
+ // Default document visibility to true for SSR
  return document.visibilityState;
};

function useDocumentVisibility(): VisibilityState {
  const [documentVisibility, setDocumentVisibility] = useState(() => getVisibility());

  useEventListener(
    'visibilitychange',
    () => {
      setDocumentVisibility(getVisibility());
    },
    {
      target: document,
    },
  );

  return documentVisibility;
}

export default useDocumentVisibility;

Default document visibility to true for SSR
@brickspert
Copy link
Collaborator

感觉并没有解决那个 issue 的问题,那个报错是 document is not defined,也就是使用了没有定义的变量。

感觉这样并没有解决吧。

@brickspert brickspert closed this Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants