I'm using react 16.8, and eslint-plugin-react-hooks 1.6.0 .When I use hooks conditionally, I hoped eslint was going to warn me. Here is my config :
"eslintConfig": {
"parser": "babel-eslint",
"plugins": [
"react-hooks"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}
If I use hooks conditionally in custom hooks, there will be a warning like this: "React Hook \"useState\" is called conditionally. React Hooks must be called in the exact same order in every component render."
function useCustomHook() {
if (Math.random() > 0.5){
const [a, setA] = useState(0)
}
}
but if I use hooks in function component, it does't work.
function MyComponent() {
if (Math.random() > 0.5){
const [a, setA] = useState(0)
}
return null
}
npm run lintbecause if you are just runningnpm startit will not show these warnings unless you update your start command to do so. Out of the box with the default example from the docs, it's setup to only warn when you run the linter. I added you conditional to my component and it warned me as it should. - Eric Bishard