-
-
Notifications
You must be signed in to change notification settings - Fork 458
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
Modify use of pause #278
Modify use of pause #278
Conversation
); | ||
|
||
// This calls executeQuery immediately during the initial mount and |
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.
Seems good but it'd be great to readd the comments and slightly rewrite them. Especially the one here that explains with this is using useImmediateEffect 😊
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.
Yup agreed - I'll add something back in 👍
changes: ReadonlyArray<any> | ||
) => { | ||
const teardown = useRef(noop); | ||
const teardown = useRef<ReturnType<EffectCallback>>(undefined); |
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.
Are we initializing this to undefined
with the expectation that teardown.current
will never get called until it gets reassigned to the return type of effect
? Seems like a safe assumption based on how this effect runs (teardown
synchronously getting re-assigned in the first if
below) but just wanted to get the reasoning on this change.
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.
Yea, should be safe since teardown functions can be undefined in the worst case 👍
useImmediateEffect(() => { | ||
if (args.pause) { |
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.
Nice I like this. So the flow here is:
- We check
args.pause
on initial mount to handle the case where a user just wants theexecuteQuery
fn. - Also check it on all subsequent changes and prevent queries when things update.
❤️Love it. My one question is should L69, if it's being treated as the cleanup function, be written as () => setState(s => ({ ...s, fetching: false }));
? Seems we're returning void
currently rather than () => void
.
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.
Two small questions but this looks awesome @andyrichardson and I can see how it solves the use case you mention ✅
The proposed changes here are aimed to simplify the use-cases associated with the
pause
argument.Rather than pause preventing requests outright - it will pause the functionality of auto fetching - whether that be on mount or when the query has changed.
The reasons for this are:
There's also a small typing fix for useImmediateEffect.
Example usage
Additional notes
The advantages of this approach are pretty clear but it does highlight a bug that we haven't yet addressed - updating variables/query values and immediately executing them in the same render doesnt act as expected.
I've created an issue about this and I believe the best way to go about this is to allow arguments to be overridden when manually fetching.