Skip to content

Commit

Permalink
Get ID from ref
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedyrose committed Feb 12, 2018
1 parent 769e1e3 commit 0df20c1
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import React from 'react'
import ReactDOM from 'react-dom'

let globalId = 0
let scriptLoaded = false

export default class HubspotForm extends React.Component {
constructor(props){
constructor(props) {
super(props)
this.state = {}
this.elId = globalId++
this.id = globalId++
this.createForm = this.createForm.bind(this)
this.createFormInterval = this.createFormInterval.bind(this)
this.loadScript = this.loadScript.bind(this)
}
createFormInterval(){
if(!this.createForm()){
createFormInterval() {
if (!this.createForm()) {
setTimeout(this.createForm, 1)
}
}
createForm(){
if(window.hbspt){
createForm() {
if (window.hbspt) {
const options = {
...this.props,
target: `#reactHubspotForm${this.elId}`,
target: `#${this.el.getAttribute('id')}`,
}
window.hbspt.forms.create(options)
return true
}
}
loadScript(){
loadScript() {
scriptLoaded = true
const script = document.createElement('script')
script.onload = this.createForm
script.src = `//js.hsforms.net/forms/v2.js`
document.head.appendChild(script)
}
componentDidMount(){
if(!scriptLoaded && !this.props.noScript){
componentDidMount() {
if (!scriptLoaded && !this.props.noScript) {
this.loadScript()
}
else{
else {
this.createFormInterval()
}
}
render(){
render() {
return (
<div id={`reactHubspotForm${this.elId}`} />
<div id={`reactHubspotForm${this.id}`} ref={el => this.el = el} />
)
}
}

0 comments on commit 0df20c1

Please sign in to comment.