-
-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement immediate effect and refactor #250
Merged
+156
−58
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
45d7f10
Add useRequest hook to simplify logic
kitten 5f05a2a
Add useImmediateEffect hook
kitten ec445bc
Fix missing return type in utils/request
kitten 4a5f3d9
Implement new helpers in useQuery
kitten 3242444
Fix useImmediateEffect teardown
kitten 176fd4b
Merge new useEffect and useImmediateEffect
kitten f7e3657
Adjust useImmediateEffect naming
kitten 94cbb37
Fix initial teardown being ignored in useImmediateEffect
kitten d069257
Update useQuery tests
kitten 262976c
Add tests for useImmediateEffect and useRequest
kitten 250b1e7
Add useRequest to useSubscription
kitten 364f1d5
Fix isMounted in hooks
kitten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react'; | ||
import { renderHook } from 'react-hooks-testing-library'; | ||
import { useImmediateEffect } from './useImmediateEffect'; | ||
|
||
it('calls effects immediately on mount', () => { | ||
const spy = jest.spyOn(React, 'useEffect'); | ||
const useEffect = jest.fn(); | ||
const effect = jest.fn(); | ||
|
||
spy.mockImplementation(useEffect); | ||
renderHook(() => useImmediateEffect(effect, [effect])); | ||
|
||
expect(effect).toHaveBeenCalledTimes(1); | ||
expect(effect).toHaveBeenCalledTimes(1); | ||
|
||
expect(useEffect).toHaveBeenCalledWith(expect.any(Function), [ | ||
expect.any(Function), | ||
]); | ||
|
||
useEffect.mockRestore(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useRef, useEffect, useCallback } from 'react'; | ||
import { noop } from '../utils'; | ||
|
||
enum LifecycleState { | ||
WillMount = 0, | ||
DidMount = 1, | ||
Update = 2, | ||
} | ||
|
||
type Effect = () => () => void; | ||
|
||
/** This executes an effect immediately on initial render and then treats it as a normal effect */ | ||
export const useImmediateEffect = ( | ||
effect: Effect, | ||
changes: ReadonlyArray<any> | ||
) => { | ||
const teardown = useRef(noop); | ||
const state = useRef(LifecycleState.WillMount); | ||
const execute = useCallback(effect, changes); | ||
|
||
// On initial render we just execute the effect | ||
if (state.current === LifecycleState.WillMount) { | ||
state.current = LifecycleState.DidMount; | ||
teardown.current = execute(); | ||
} | ||
|
||
useEffect(() => { | ||
// Initially we skip executing the effect since we've already done so on | ||
// initial render, then we execute it as usual | ||
if (state.current === LifecycleState.Update) { | ||
return (teardown.current = execute()); | ||
} else { | ||
state.current = LifecycleState.Update; | ||
return teardown.current; | ||
} | ||
}, [execute]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { renderHook } from 'react-hooks-testing-library'; | ||
import { queryGql } from '../test-utils'; | ||
import { useRequest } from './useRequest'; | ||
|
||
it('preserves instance of request when key has not changed', () => { | ||
let { query, variables } = queryGql; | ||
|
||
const { result, rerender } = renderHook( | ||
({ query, variables }) => useRequest(query, variables), | ||
{ initialProps: { query, variables } } | ||
); | ||
|
||
const resultA = result.current; | ||
expect(resultA).toEqual({ | ||
key: expect.any(Number), | ||
query: expect.anything(), | ||
variables: variables, | ||
}); | ||
|
||
variables = { ...variables }; // Change reference | ||
rerender({ query, variables }); | ||
|
||
const resultB = result.current; | ||
expect(resultA).toBe(resultB); | ||
|
||
variables = { ...variables, test: true }; // Change values | ||
rerender({ query, variables }); | ||
|
||
const resultC = result.current; | ||
expect(resultA).not.toBe(resultC); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DocumentNode } from 'graphql'; | ||
import { useRef, useMemo } from 'react'; | ||
import { GraphQLRequest } from '../types'; | ||
import { createRequest } from '../utils'; | ||
|
||
/** Creates a request from a query and variables but preserves reference equality if the key isn't changing */ | ||
export const useRequest = ( | ||
query: string | DocumentNode, | ||
variables?: any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would you feel about using a generic here so we can pass it through from |
||
): GraphQLRequest => { | ||
const prev = useRef<void | GraphQLRequest>(undefined); | ||
|
||
return useMemo(() => { | ||
const request = createRequest(query, variables); | ||
// We manually ensure reference equality if the key hasn't changed | ||
if (prev.current !== undefined && prev.current.key === request.key) { | ||
return prev.current; | ||
} else { | ||
prev.current = request; | ||
return request; | ||
} | ||
}, [query, variables]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might be unintentionally shadowing
query
andvariables
here. Typically I'll define what's ininitialProps
to be different from my test data to ensure that all calls torerender
are using the data passed ininitialProps
orrerender
, i.e.:Functionally it doesn't matter b/c
query
(insiderenderHook
) ->initialProps.query
->query
(fromqueryGql
), and we uselet
bindings to update the references tovariables
below, so this is more of a readability suggestion to make it clear where things are coming from 🤗