Closed snelsi closed 3 years ago
Interesting. I'm happy to accept PRs for this.
@aaronhayes Sure, here you go https://github.com/aaronhayes/react-use-hubspot-form/pull/26
That is fantastic! Thank you. Released in version 2.0.1
I just noticed this bug while working for a client project and was just about to delve into how to fix it - then I spotted this. Thanks @snelsi !
🐛 I'm submitting a Bug report
Summary
If two
useHubspotForm
hooks are called on the same page, the second form sometimes may never load.Background
I'm using the
useHubspotForm
hook with Next.js a couple of times on the same page. It looks like sometimes only 1 form is rendered properly, and others never load.While inspecting React dev tools, I noticed that script is loaded and there is no error, but
formCreated
is set to false.I copied the source code locally and tried to console log hook's state to debug the error:
It looks like even if
loaded
prop returnstrue
,window.hbspt
can still beundefined
. And when the script is actually loadeduseEffect
withhbspt.forms.create()
does not trigger.Cause of the bug
Here's the problematic code:
Possible solution
You use
useScript
in one place, for loadinghttps://js.hsforms.net/forms/v2.js
script. So it may have more sense to move this logic into a separateHubspotProvider
component and use React context to syncloaded / error
state between all Hubspot forms: