From e8819437887facec1bf9d3f884cb329adc39f62a Mon Sep 17 00:00:00 2001 From: Jan Hrdina Date: Sun, 18 Mar 2018 10:17:38 +0100 Subject: [PATCH] Remove rendered DOM on unmount - fixes #55 --- src/recaptcha.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index 48266ed..91b36b8 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -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, @@ -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); @@ -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; }