avoid reference errors when dynamically loading hooks after create ho…#933
Merged
avoid reference errors when dynamically loading hooks after create ho…#933
Conversation
Contributor
Author
|
verified on my reproduction app that using the patch versions from #934 fixed the issue |
airhorns
approved these changes
Dec 3, 2025
Contributor
airhorns
left a comment
There was a problem hiding this comment.
Crap, thanks for fixing this, this makes sense.
| useAction = (action, options) => { | ||
| let useActionImpl: UseAction = createHookStub("useAction"); | ||
|
|
||
| createHookStub("useAction", (adapter, coreHooks) => { |
Contributor
There was a problem hiding this comment.
Dunno if you particularly care, but you could make createHookStub not actually do any variable rebinding in each module, and instead do it internally. Like:
const useAction: UseAction = createHookStub("useAction", (adapter, coreHooks) => {
return (action, options) => {
...
}
}and then
const createHookStub = (factory) => {
let resultFn;
if (immediate) {
resultFn = factory(....)
} else {
let impl;
resultFn = (...args) => {
if (impl) {
return impl(...args);
} else {
throw
}
}
onGivenFactory(() => {
impl = factory()
});
}
return resultFn;
}or some such
Contributor
Author
There was a problem hiding this comment.
yah that was one of the options that I explored with cursor; opus suggested the option in this PR was
My Recommendation: Pattern 1 (Internal Impl + Wrapper)
Pattern 1 is the cleanest and requires minimal changes:
5772e73 to
385ad9c
Compare
…oks has been called
385ad9c to
856e877
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Users have run into issues of production builds of their application when after initial react router page load they navigate to a route which imports a new hook. A fix for this was added in #926 however it seems in many cases this leads to a
ReferenceError: Cannot access 'useFindFirst' before initializationThis is due to something I just learned today had a name temporary dead zone
The issue is that when we i.e.
import { useFindFirst } from "@gadgetinc/react"when the react provider has been set up BUT none of the hook modules have been imported we end effectively running this code:this immediately invokes the inner function in the block that is defining useFindFirst and this tries to assign to
useFindFirstwhich is in the TDZthe fix is to keep the exported useFindFirst binding stable and have createHookStub update an internal impl variable, rather than doing useFindFirst = … inside the initializer.
PR Checklist