Skip to content

Commit

Permalink
set dispatchRef in render to avoid useLayoutEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Nov 28, 2019
1 parent 1305b2e commit 5da7357
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": ["error", { "additionalHooks": "useIsomorphicLayoutEffect" }],
"react-hooks/exhaustive-deps": "error",
"@typescript-eslint/explicit-function-return-type": "off",
"react/jsx-filename-extension": ["error", { "extensions": [".js", ".tsx"] }],
"react/prop-types": "off",
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased]
### Changed
- Use useIsomorphicLayoutEffect hack to avoid warning in SSR
- Do not useLayoutEffect which shows warning in SSR

## [3.8.0] - 2019-11-26
### Changed
Expand Down
8 changes: 3 additions & 5 deletions src/use-async-task.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { useEffect, useLayoutEffect, useReducer, useRef } from 'react';

const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
import { useEffect, useReducer, useRef } from 'react';

const createTask = ({ func, dispatchRef }) => {
const taskId = Symbol('TASK_ID');
Expand Down Expand Up @@ -100,8 +98,8 @@ export const useAsyncTask = (func) => {
if (task.func !== func) {
dispatch({ type: 'INIT', func, dispatchRef });
}
useIsomorphicLayoutEffect(() => {
dispatchRef.current = dispatch;
dispatchRef.current = dispatch;
useEffect(() => {
const cleanup = () => {
dispatchRef.current = () => {};
};
Expand Down

0 comments on commit 5da7357

Please sign in to comment.