jsx-eslint/eslint-plugin-react

Rule proposal: ssr-friendly

Open

#2057 aperta il 26 nov 2018

Vedi su GitHub
 (21 commenti) (23 reazioni) (0 assegnatari)JavaScript (2797 fork)batch import
acceptedhelp wantednew rule

Metriche repository

Star
 (8630 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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

Guida contributor