Coder Social home page Coder Social logo

eslint-plugin-react-hooks-ssr's Introduction

eslint-plugin-react-hooks-ssr

This plugin helps you to forbid DOM globals within the react server side rendering.

  • it doesn't support yet React classes
  • it supports react hooks and custom hooks
  • it requires some naming conventions to identify other functions where globals may be allowed

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-react-hooks-ssr:

$ npm install eslint-plugin-react-hooks-ssr --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-react-hooks-ssr globally.

Usage

Add react-hooks-ssr to the plugins section of your .eslintrc.js configuration file. You can omit the eslint-plugin- prefix:

{
    "plugins": [
        "react-hooks-ssr"
    ]
}

Optionally configure the regexp to whitelist globals within certain function declarations (by default the async prefix).

{
    "rules": {
        "react-hooks-ssr/react-hooks-global-ssr": ["error", { "allowFuncRegExp": /test/ }]
    }
}

Documentation

  • a global within useEffect is allowed
function Component() {
    useEffect(() => {
        console.log(window.innerWidth);
    });
    return <div>Hello</div>;
}
  • a global within a custom hook (useXXX) is allowed
function Component() {
    useCustomHook(() => {
        console.log(window.innerWidth);
    });
    return <div>Hello</div>;
}
  • a global within a function prefixed by async (asyncMyFunc) is allowed. This pattern can be replaced by the allowFuncRegExp option
function asyncMyFunction() {
  console.log(window.innerWidth);
}
  • a global within a useState, useReducer and useMemo callback is forbidden
function Component() {
    const [myState, setMyState] = useState(() => {
        return window.innerWidth
    });
    return <div>Hello</div>;
}
  • a global within a React Component is forbidden
function Component() {
    console.log(window.innerWidth)
    return <div>Hello</div>;
}

eslint-plugin-react-hooks-ssr's People

Contributors

correttojs avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.