Skip to content

Commit

Permalink
feat: no config serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneb committed Mar 6, 2021
1 parent 4176d94 commit 15fe158
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 5 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 @@ -34,6 +34,7 @@
},
"dependencies": {
"@babel/runtime": "7.13.9",
"dequal": "2.0.2",
"lru-cache": "6.0.0"
},
"peerDependencies": {
Expand Down
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import StaticAxios from 'axios'
import LRU from 'lru-cache'
import { dequal as deepEqual } from 'dequal/lite'

const actions = {
REQUEST_START: 'REQUEST_START',
Expand Down Expand Up @@ -197,17 +198,17 @@ export function makeUseAxios(configureOptions) {
)
}

function useAxios(config, options) {
config = React.useMemo(
() => configToObject(config),
function useAxios(_config, _options) {
const config = React.useMemo(
() => configToObject(_config),
// eslint-disable-next-line react-hooks/exhaustive-deps
[JSON.stringify(config)]
useDeepCompareMemoize(_config)
)

options = React.useMemo(
() => ({ ...defaultOptions, ...options }),
const options = React.useMemo(
() => ({ ...defaultOptions, ..._options }),
// eslint-disable-next-line react-hooks/exhaustive-deps
[JSON.stringify(options)]
useDeepCompareMemoize(_options)
)

const cancelSourceRef = React.useRef()
Expand Down Expand Up @@ -267,3 +268,15 @@ export function makeUseAxios(configureOptions) {
return [state, refetch, cancelOutstandingRequest]
}
}

function useDeepCompareMemoize(value) {
const ref = React.useRef()
const signalRef = React.useRef(0)

if (!deepEqual(value, ref.current)) {
ref.current = value
signalRef.current += 1
}

return [signalRef.current]
}

0 comments on commit 15fe158

Please sign in to comment.