Skip to content

Instantly share code, notes, and snippets.

@lintonye
Last active February 20, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lintonye/6a6d7fc7cf8d6603a0878a2327bf9407 to your computer and use it in GitHub Desktop.
Save lintonye/6a6d7fc7cf8d6603a0878a2327bf9407 to your computer and use it in GitHub Desktop.
import React, { PureComponent } from 'react';
const IS_BROWSER = typeof window !== "undefined";
export default class Form extends PureComponent {
componentWillMount() {
const { formId, version = 6 } = this.props;
if (IS_BROWSER) {
this.script = document.createElement("script");
this.script.id = `_ck_${formId}`;
this.script.async = true;
this.script.src = `https://forms.convertkit.com/${formId}?v=${version}`;
}
}
componentDidMount() {
if (IS_BROWSER) {
// Make sure the convertKit inserts form elements again when the component is unmounted & re-mounted.
// This is useful to embed the form in for example a modal. Without this line, the sign up form will
// only appear the first time when the modal is open.
window._CK_processed = undefined;
this.div.appendChild(this.script);
}
}
saveDiv = div => (this.div = div);
render() {
return <div ref={this.saveDiv} />;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment