Skip to content

Commit

Permalink
feat: maintain referential integrity of refetch function
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneb committed Oct 22, 2019
1 parent 591eb2b commit 1c4c5ac
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
8 changes: 5 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugin:import/recommended",
"plugin:prettier/recommended"
],
"plugins": ["import", "react-hooks"],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
Expand All @@ -18,7 +19,8 @@
"Promise": true
},
"rules": {
"valid-jsdoc": 2
},
"plugins": ["import"]
"valid-jsdoc": 2,
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"eslint-config-prettier": "6.4.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-prettier": "3.1.1",
"eslint-plugin-react-hooks": "2.1.2",
"husky": "3.0.8",
"jest": "24.9.0",
"lint-staged": "9.4.2",
Expand Down
22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export default function useAxios(config, options) {
}
}

const stringifiedConfig = JSON.stringify(config)

options = { manual: false, useCache: true, ...options }

const [state, dispatch] = React.useReducer(
Expand All @@ -138,14 +140,20 @@ export default function useAxios(config, options) {
if (!options.manual) {
executeRequest(config, options, dispatch)
}
}, [JSON.stringify(config)])
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [stringifiedConfig])

return [
state,
const refetch = React.useCallback(
(configOverride, options) => {
options = { useCache: false, ...options }
return executeRequest(
{ ...config, ...configOverride },
{ useCache: false, ...options },
dispatch
)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[stringifiedConfig]
)

return executeRequest({ ...config, ...configOverride }, options, dispatch)
}
]
return [state, refetch]
}
12 changes: 12 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ it('should refetch', async () => {
expect(axios).toHaveBeenCalledTimes(2)
})

it('should return the same reference to the fetch function', async () => {
axios.mockResolvedValue({ data: 'whatever' })

const { result, rerender } = renderHook(() => useAxios(''))

const firstRefetch = result.current[1]

rerender()

expect(result.current[1]).toBe(firstRefetch)
})

describe('manual option', () => {
it('should set loading to false', async () => {
const { result } = renderHook(() => useAxios('', { manual: true }))
Expand Down

0 comments on commit 1c4c5ac

Please sign in to comment.