0

我正在尝试将 Sharpspring 表单脚本嵌入到我的 React (page.jsx) 组件中。要使此表单成功加载,它需要放置元素<head></head>之外,并且<div>在我要显示表单的位置内。

我需要嵌入的原始 HTML 代码如下所示:

<!-- SharpSpring Form  -->
<script type="text/javascript">
    var ss_form = {'account': 'MzawMDE3tzQzBQ', 'formID': 'szBIMrFMSzbXTUm0TNM1MTc107VMNbfQTTRJTExOTUoySk4zAA'};
    ss_form.width = '100%';
    ss_form.domain = 'app-3QNK98WC9.marketingautomation.services';
    // ss_form.hidden = {'field_id': 'value'}; // Modify this for sending hidden variables, or overriding values
    // ss_form.target_id = 'target'; // Optional parameter: forms will be placed inside the element with the specified id
    // ss_form.polling = true; // Optional parameter: set to true ONLY if your page loads dynamically and the id needs to be polled continually.
</script>
<script type="text/javascript" src="https://koi-3QNK98WC9.marketingautomation.services/client/form.js?ver=2.0.1"></script>
                

我正在尝试以这种方式将其嵌入到 page.jsx 中:

import React from "react";

function myComponent()  {
  
  var ss_form = {'account': 'MzawMDE3tzQzBQA', 'formID': 'szBIMrFMSzbXTUm0TNM1MTc107VMNbfQTTRJTExOTUoySk4zAAA'};
  ss_form.width = '100%';
  ss_form.domain = 'app-3QNK98WC9Y.marketingautomation.services';
  
  return (

   <main>
    <div className="form">
       {ss_form}
        <script src="https://koi-3QNK98WC9Y.marketingautomation.services/client/form.js?ver=2.0.1" type="text/javascript" />
     </div>  
   </main>

  );
}

export default myComponent;

但是,我收到此错误:

Error: Objects are not valid as a React child (found: object with keys {account, formID, width, domain}). If you meant to render a collection of children, use an array instead.

我知道我应该使用“数组”,但我找不到任何解决此错误的文档。关于如何使这个嵌入代码工作的任何建议?

谢谢你。

4

1 回答 1

0

我能够使用头盔嵌入表单。

首次安装:

npm install helmet

我的工作代码如下所示:

import React from "react";
import ReactDOM from "react-dom";
import { Helmet } from 'react-helmet';

  function myComponent() {
    return (
    <main>
    <Helmet>
      <script
          src="https://koi-3QNK98WC9.marketingautomation.services/client/form.js?ver=2.0.1"
        />
        <script>
          {`
              var ss_form = {'account': 'MzawMDE3tzQzBQ', 'formID': 'szBIMrFMSzbXTUm0TNM1MTc107VMNbfQTTRJTExOTUoySk4zAA'};
              ss_form.width = '100%';
              ss_form.domain = 'app-3QNK98WC9Y.marketingautomation.services';
              ss_form.target_id = 'form'; 
              ss_form.polling = true; 
            
          `}
        </script>
      </Helmet>
   
      <div id="form"> </div>

  </main>
  );
}

export default myComponent;

原来 SharpSpring 嵌入代码支持变体ss_form.target_id,它在指定的 ID 上显示表单。

我确信上述解决方案也适用于其他 javascript 和嵌入代码。

于 2021-11-27T20:21:26.050 回答