在 React 应用程序中嵌入 Typeform

Eli*_*ant 6 reactjs typeform

如何在我的 React 应用程序中嵌入 Typeform 表单?

Typeform 提供的嵌入代码无法在 JSX 中编译。

这是嵌入代码的示例:

<div class="typeform-widget" data-url="https://sample.typeform.com/to/32423" style="width: 100%; height: 500px;"></div> <script> (function() { var qs,js,q,s,d=document, gi=d.getElementById, ce=d.createElement, gt=d.getElementsByTagName, id="typef_orm", b="https://embed.typeform.com/"; if(!gi.call(d,id)) { js=ce.call(d,"script"); js.id=id; js.src=b+"embed.js"; q=gt.call(d,"script")[0]; q.parentNode.insertBefore(js,q) } })() </script> <div style="font-family: Sans-Serif;font-size: 12px;color: #999;opacity: 0.5; padding-top: 5px;"> powered by <a href="https://admin.typeform.com/signup?utm_campaign=ezQ2ub&utm_source=typeform.com-12183356-Basic&utm_medium=typeform&utm_content=typeform-embedded-poweredbytypeform&utm_term=EN" style="color: #999" target="_blank">Typeform</a> </div>

Eli*_*ant 8

您可以在此处查看用于嵌入 JavaScript 的 Typeform 文档。

他们的官方 npm 模块在这里

使用React refs触发初始化,类似于初始化 jQuery 插件的方式。

import React from 'react';
import * as typeformEmbed from '@typeform/embed'

class Form extends React.Component {
  constructor(props) {
    super(props);
    this.el = null;
  }
  componentDidMount() {
    if (this.el) {
      typeformEmbed.makeWidget(this.el, "https://sample.typeform.com/to/32423", {
        hideFooter: true,
        hideHeaders: true,
        opacity: 0
      });
    }
  }
  render() {
    return (
      <div ref={(el) => this.el = el} style={{width: '100%', height: '300px'}} />
    )
  }
}

export default Form;
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以使用我创建的 React 包装器组件:https : //github.com/alexgarces/react-typeform-embed

它在引擎盖下使用官方的 Typeform Embed SDK。基本上你必须传递你的表单 url,但它也支持其他 SDK 选项。

import React from 'react';
import { ReactTypeformEmbed } from 'react-typeform-embed';

class App extends React.Component {
  render() {
    return <ReactTypeformEmbed url="https://demo.typeform.com/to/njdbt5" />;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了这个并收到错误:“无法找到模块“react-typeform-embed”的声明文件。'xyz_path/node_modules/react-typeform-embed/lib/index.js' 隐式具有 'any' 类型。 (2认同)