Skip to content

Commit

Permalink
Remove rendered DOM on unmount
Browse files Browse the repository at this point in the history
- fixes #55
  • Loading branch information
jhrdina committed Mar 18, 2018
1 parent 73edf7f commit e881943
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/recaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default class ReCAPTCHA extends React.Component {

explicitRender(cb) {
if (this.props.grecaptcha && this.state.widgetId === undefined) {
const id = this.props.grecaptcha.render(this.captcha, {
const wrapper = document.createElement("div");
const id = this.props.grecaptcha.render(wrapper, {
sitekey: this.props.sitekey,
callback: this.props.onChange,
theme: this.props.theme,
Expand All @@ -61,6 +62,8 @@ export default class ReCAPTCHA extends React.Component {
stoken: this.props.stoken,
badge: this.props.badge,
});
this.captcha.appendChild(wrapper);

this.setState({
widgetId: id,
}, cb);
Expand All @@ -79,6 +82,15 @@ export default class ReCAPTCHA extends React.Component {
this.explicitRender();
}

componentWillUnmount() {
if (this.state.widgetId !== undefined) {
while (this.captcha.firstChild) {
this.captcha.removeChild(this.captcha.firstChild);
}
this.reset();
}
}

handleRecaptchaRef(elem) {
this.captcha = elem;
}
Expand Down

0 comments on commit e881943

Please sign in to comment.