# Can't render hubspot form in react module

**URL:** <https://community.hubspot.com/t/cant-render-hubspot-form-in-react-module/136269>\
**Category:** CMS Development\
**Tags:** professional\
**Created:** [July 15, 2025, 3:53pm UTC](https://community.hubspot.com/t/cant-render-hubspot-form-in-react-module/136269 "2025-07-15T15:53:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shatak1](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Shatak1](https://community.hubspot.com/u/Shatak1)\
**Post date:** [July 15, 2025, 3:53pm UTC](https://community.hubspot.com/t/cant-render-hubspot-form-in-react-module/136269/1 "2025-07-15T15:53:12Z")

</div>

import React, { useState, useEffect } from ‘react’;

import ‘./modal.scss’;

import ‘./hsForm.scss’;

function Modal({ userBMI, modal, setopenModal, needLoose, needGain }) {

const [isFormLoading, setIsFormLoading] = useState(true);

useEffect(() =\> {

```
const formId = modal?.popup\_form?.form\_id;

const script = document.createElement('script');

script.src = 'https://js.hsforms.net/forms/v2.js';

script.async = true;

script.defer = true;

const handleScriptLoad = () => {

  if (window.hbspt) {

    window.hbspt.forms.create({

      region: "na1",

      portalId: portalId,

      formId: formId, **//if i use form\_id dynamic its not working , only form button is rendering no form is showing**

      formId: 'when i use my form ID hardcoded here its working, form is rendering ',

      target: '#hubspotFormContainer',

      onFormReady: function($form) {

        console.log('HubSpot form has successfully loaded.');

        setIsFormLoading(false);

      }

    });

  }

};

script.addEventListener('load', handleScriptLoad);

document.body.appendChild(script);

return () => {

  script.removeEventListener('load', handleScriptLoad);

  if (script.parentNode) {

    script.parentNode.removeChild(script);

  }

  const formContainer = document.querySelector('#hubspotFormContainer');

  if (formContainer) {

    formContainer.innerHTML = '';

  }

};

```

}, [formId]);

const closeModal = () =\> {

```
setopenModal(false);

```

};

return (

```
\<div className="modal-overlay">

  \<div className="modal-content">

    \<div className="modal-header">

      \<h2>{modal.popup\_title}\</h2>

    \</div>

    \<div className="modal-body">

      \<div className="result">

        \<div className='result-container'>

          \<div className='bmi'>

            \<div className='bmi-text body-1'>\<p>Your BMI:\</p>\</div>

            \<div className='bmi-value'>\<h4>{userBMI}\</h4>\</div>

          \</div>

          \<div className='status'>

            \<div className='status-text body-1'>\<p>Status:\</p>\</div>

            \<h4 className='status-value'>

              {userBMI > 0 && (

                \<p>

                  {userBMI < 18.5 ? modal.status\[0].status :

                   userBMI < 25 ? modal.status\[1].status :

                   modal.status\[2].status}

                \</p>

              )}

            \</h4>

          \</div>

        \</div>

        \<div className='suggest body-1'>

          {userBMI > 25 ?

            \<p>To achieve a BMI of 25: {modal.to\_lose} {needLoose}kg\</p> :

            userBMI < 18.5 ?

            \<p>To achieve a BMI of 18.5: {modal.to\_gain} {needGain}kg\</p> :

            'Your BMI is in a healthy range.'}

        \</div>

        \<div className="consultation-message body-1">

          \<p>{modal.description}\</p>

        \</div>

      \</div>

    \</div>

    \<div

      id="hubspotFormContainer"

      className='form'

    >

    \</div>

  \</div>

\</div>

```

);

**Problem : When i use dynamic form id that i pulled from module data, form is not rendering in my webpage, but if i use hardcoaded form\_id in the formID it works. How to solve this issue**

---

<div class="post-metadata">

**Author:** ![FaramarKolivand](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/faramarkolivand/32/76210_2.png) [@FaramarKolivand](https://community.hubspot.com/u/FaramarKolivand)\
**Post date:** [July 15, 2025, 7:32pm UTC](https://community.hubspot.com/t/cant-render-hubspot-form-in-react-module/136269/2 "2025-07-15T19:32:13Z")

</div>

Could it be timing? You’re using a dynamic formId, maybe it’s undefined or not available yet at the time your script is running.

---

<div class="post-metadata">

**Author:** ![Shatak1](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Shatak1](https://community.hubspot.com/u/Shatak1)\
**Post date:** [July 16, 2025, 7:48am UTC](https://community.hubspot.com/t/cant-render-hubspot-form-in-react-module/136269/3 "2025-07-16T07:48:51Z")

</div>

I also used the delay like 3-4s but only dynamic form button is loading in UI. No input feilds. Also I have another qeuestion that can we use custom css in my hubspot form in react module ?

---

<div class="post-metadata">

**Author:** ![FaramarKolivand](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/faramarkolivand/32/76210_2.png) [@FaramarKolivand](https://community.hubspot.com/u/FaramarKolivand)\
**Post date:** [July 16, 2025, 8:14pm UTC](https://community.hubspot.com/t/cant-render-hubspot-form-in-react-module/136269/4 "2025-07-16T20:14:09Z")

</div>

Does your actual code have two “formId” or did you include the second one to explain the issue here? If you have two instances in your code then the second one will overwrite the first one. Can you give this a try?

```lia-code-sample
useEffect(() => {
 const portalId = 'your-portal-id'; // Make sure this is valid
 const formId = modal?.popup_form?.form_id;

 if (!formId || !portalId) return; // Don't proceed if not ready

 const createForm = () => {
 if (window.hbspt) {
 window.hbspt.forms.create({
 region: 'na1',
 portalId,
 formId,
 target: '#hubspotFormContainer',
 cssClass: 'custom-hs-form',
 onFormReady: function ($form) {
 console.log('HubSpot form has successfully loaded.');
 setIsFormLoading(false);
 }
 });
 }
 };

 // If hbspt already loaded
 if (window.hbspt) {
 createForm();
 } else {
 const script = document.createElement('script');
 script.src='https://js.hsforms.net/forms/v2.js';
 script.async = true;
 script.defer = true;
 script.onload = createForm;
 document.body.appendChild(script);
 }

 return () => {
 const formContainer = document.querySelector('#hubspotFormContainer');
 if (formContainer) {
 formContainer.innerHTML = '';
 }
 };
}, [modal?.popup_form?.form_id]);

```

Regarding your CSS question, if the form is injected with iframe, then you cannot directly style inside an iframe from outside with plain CSS. Use the form settings, or try passing a cssClass to the outer form wrapper if it helps.
