Skip to content

Commit e881943

Browse files
committed
Remove rendered DOM on unmount
- fixes dozoisch#55
1 parent 73edf7f commit e881943

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/recaptcha.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export default class ReCAPTCHA extends React.Component {
5050

5151
explicitRender(cb) {
5252
if (this.props.grecaptcha && this.state.widgetId === undefined) {
53-
const id = this.props.grecaptcha.render(this.captcha, {
53+
const wrapper = document.createElement("div");
54+
const id = this.props.grecaptcha.render(wrapper, {
5455
sitekey: this.props.sitekey,
5556
callback: this.props.onChange,
5657
theme: this.props.theme,
@@ -61,6 +62,8 @@ export default class ReCAPTCHA extends React.Component {
6162
stoken: this.props.stoken,
6263
badge: this.props.badge,
6364
});
65+
this.captcha.appendChild(wrapper);
66+
6467
this.setState({
6568
widgetId: id,
6669
}, cb);
@@ -79,6 +82,15 @@ export default class ReCAPTCHA extends React.Component {
7982
this.explicitRender();
8083
}
8184

85+
componentWillUnmount() {
86+
if (this.state.widgetId !== undefined) {
87+
while (this.captcha.firstChild) {
88+
this.captcha.removeChild(this.captcha.firstChild);
89+
}
90+
this.reset();
91+
}
92+
}
93+
8294
handleRecaptchaRef(elem) {
8395
this.captcha = elem;
8496
}

0 commit comments

Comments
 (0)