jsx-eslint/eslint-plugin-react

Rule proposal: ssr-friendly

Aberta

#2.057 aberto em 26 de nov. de 2018

 (21 comentários) (23 reações) (0 responsável)JavaScript (2.731 forks)batch import
acceptedhelp wantednew rule

Métricas do repositório

Stars
 (9.293 estrelas)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

In order to be SSR compliant, you can only access browser globals such as window, document and so on, inside of componentDidMount, componentDidUpdate and componentWillUnmount

A rule could be created to avoid the use of unguarded browser globals in other lifecycle methods.

Example of a guard:

if (typeof window !== 'undefined') {
 // I can use browser globals in here
}

Use of browser globals are allowed when:

  • used in a guard
  • used in componentDidMount, componentDidUpdate and componentWillUnmount
  • used in useEffect or useLayoutEffect
  • used in an event handler

Guia do colaborador