gpt4 book ai didi

javascript - 如何在 react js 应用程序中插入 hubspot 表单?

转载 作者:行者123 更新时间:2023-12-05 00:31:31 27 4
gpt4 key购买 nike

我有一个 react js 网站,我想插入一个 hubspot 表单。
Hubspot形式:

<script charset="utf-8" type="text/javascript" src="//js-eu1.hsforms.net/forms/shell.js"></script>
<script>
hbspt.forms.create({
region: "eu1",
portalId: "25082527",
formId: "8a7c2adb-5c41-4c47-8478-b1f1abe01e3f"
});
</script>
react js页面联系方式
import React from 'react';
import SEO from "../common/SEO";
import Layout from "../common/Layout";
import ContactOne from "../elements/contact/ContactOne";

const ContactPage = () => {
return (
<>
<SEO title="Contact" desc="EXIGOH - Contact us now"/>
<Layout>
<div className="main-content">
{/* Start Contact Area */}
<div className="rwt-contact-area rn-section-gap">


<div className="container">
<div className="row">
<div className="col-lg-12 mb--40">
<div className="text-center">
<h1 className="title theme-gradient h2" dangerouslySetInnerHTML={{__html: 'The Easiest Way To Start <br /> fill in & send.'}}/>
</div>
</div>
</div>

  **<<<<<<<< -------------- insert it here >>**

{/*<ContactOne/>*/}
</div>
</div>
{/* End Contact Area */}
</div>
</Layout>
</>
)
}
export default ContactPage;
所以我的问题是:我该怎么做?我尝试使用 <helmet>但这对我不起作用。如何在 react js 中插入脚本标签?

最佳答案

这就是我所做的,就像一个魅力......

  • 创建一个名为 HubspotContactForm 的组件

  •     import React, {useEffect} from "react";

    const HubspotContactForm = props => {
    const { region, portalId, formId } = props;
    useEffect(() => {
    const script = document.createElement('script');
    script.src='https://js.hsforms.net/forms/shell.js';
    document.body.appendChild(script);

    script.addEventListener('load', () => {
    // @TS-ignore
    if (window.hbspt) {
    // @TS-ignore
    window.hbspt.forms.create({
    region: {region},
    portalId: {portalId},
    formId: {formId},
    target: '#hubspotForm'
    })
    }
    });
    }, []);

    return (
    <div>
    <div id="hubspotForm"></div>
    </div>
    );
    };

    export default HubspotContactForm;
  • 导入此组件并在页面上使用它:

  • import {
    HubspotContactForm,
    } from '../components';

    export default function MyPage() {
    return (
    <main>
    <div className="container">
    <div className="row">
    <div className="col">
    <HubspotContactForm
    region="na1"
    portalId="12345678"
    formId='4333d218-20e3-41f9-a478-47bd0c26bf1a'
    />
    </div>
    </div>
    </div>
    </main>
    );
    };

    关于javascript - 如何在 react js 应用程序中插入 hubspot 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69317628/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com