Skip to content
This repository has been archived by the owner on Dec 27, 2019. It is now read-only.

Commit

Permalink
deep equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Dec 4, 2018
1 parent fd8e829 commit 56e0019
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/screens/user/components/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useContext, useReducer, useEffect} from 'react'
import {useContext, useReducer, useEffect, useRef} from 'react'
import PropTypes from 'prop-types'
import isEqual from 'lodash/isEqual'
import * as GitHub from '../../../github-client'

function Query({query, variables, normalize = data => data, children}) {
Expand All @@ -14,30 +15,35 @@ function Query({query, variables, normalize = data => data, children}) {
},
)

useEffect(
() => {
setState({fetching: true})
client
.request(query, variables)
.then(res =>
setState({
data: normalize(res),
error: null,
loaded: true,
fetching: false,
}),
)
.catch(error =>
setState({
error,
data: null,
loaded: false,
fetching: false,
}),
)
},
[query, variables],
)
useEffect(() => {
if (isEqual(previousInputs.current, [query, variables])) {
return
}
setState({fetching: true})
client
.request(query, variables)
.then(res =>
setState({
data: normalize(res),
error: null,
loaded: true,
fetching: false,
}),
)
.catch(error =>
setState({
error,
data: null,
loaded: false,
fetching: false,
}),
)
})

const previousInputs = useRef()
useEffect(() => {
previousInputs.current = [query, variables]
})

return children(state)
}
Expand Down

0 comments on commit 56e0019

Please sign in to comment.