I’m having trouble embedding a hubspot form into a React component. JSX will consider the script tag as a React component itself and try to compile it as such. Have you guys tried to integrate a hubspot form with react before?
This is great. Worked perfectly for us right away. Thank you!
Here is a typescript friendly version (really just supressing the warnings) that uses a functional component and useEffect instead of class component and componentDidMount.
I took a different (slightly more complicated) approach but ended up a functioning form. I just added the base script to a Helmet tag and added the window.hbspt parameter for useEffect.
that work for me and was so easy, also i recomend remove strict mode from your index, i got a problem with that i dont know why but my form was rendering twice, i just removed that and that way i fixed the form, if have question reply 😃
that work for me and was so easy, also i recomend remove strict mode from your index, i got a problem with that i dont know why but my form was rendering twice, i just removed that and that way i fixed the form, if have question reply 😃
Hi everyone, I struggled through this issue a bit, but after referencing a couple of different posts online I’ve come to a working solution. Please reference my code example: https://jsfiddle.net/2Lwq9zbg/
This is great. Worked perfectly for us right away. Thank you!
Here is a typescript friendly version (really just supressing the warnings) that uses a functional component and useEffect instead of class component and componentDidMount.
Jan 18, 20238:47 AM - edited Jan 18, 20239:45 AM
Participant
HELP: Embed a form in a React component
SOLVE
I try this code in Next.js application, the script gets loaded, hbspt.form.create gets executed but the form doesn't show up, so the target remains unchanged. The solution that worked for me was the answer based on class components.
I took a different (slightly more complicated) approach but ended up a functioning form. I just added the base script to a Helmet tag and added the window.hbspt parameter for useEffect.
Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success. Don't miss this opportunity to connect and grow—reserve your spot today!
Did you know that the Community is available in other languages? Join regional conversations by changing your language settings !
I just spun something up really quickly. Here is my code so I hope this helps. You can’t unfortunately use script tags in a render function as noted above. Let me know if this solves your problem or not. The first part calls the HubSpot script from our servers and the second part will then use the contents of said script to place the form in your component. I had to use a later lifecylce stage to get it to work. I was hoping it would have worked with componentDidMount(){} but it seemed to be firing to fast before the WillMount() could full finish loading the script.
export default class Foo extends Component {
componentWillMount(){
const script = document.createElement("script");
script.src = "https://js.hsforms.net/forms/v2.js";
script.async = true;
document.body.appendChild(script);
}
componentDidUpdate(){
//you will need to change the below to match your portal ID and your form ID
hbspt.forms.create({
portalId: '2323210',
formId: 'd8232cf6-b9ed-4abc-a81e-585801849cea'
});
}
render() {
I am new to react …but How should we use the render function in that case?
Basically what should be inserted in the render method so that it get the information from the componentDidUpdate method?
Thanks for that. I implemented it as per your instructions but unfortunately that still doesn’t work. It throws the error that hbspt doesn’t exist, when this runs as the library is probably not fully loaded yet when I’m calling the forms.create function.
I’ve solved it by hardcoding the first script part where the library is loaded onto my root HTML (in Rails) and then moving the function call into the componentDidMount.