I'm using react-google-recaptcha
for a simple contact form on Next.js. The app theme can be toggled using a button that triggers a function that appends 'dark' to html as a class and adds a variable in localStorage. How do I get the recaptcha to update with this?
The problem is to check for dark mode I need to access either window
to check for html appended class or localStorage
to retrieve a dark-mode value I append on theme switch. This means I can only use the componentDidMount
lifecycle method which only fires once. (SSR)
I would need something that could dynamically inject the theme string when either of the above values changes and remount the component. Here is my Captcha.jsx
component which I am importing into my contact.jsx
page.
import React, { Component } from 'react';
import ReCAPTCHA from 'react-google-recaptcha';
export default class Captcha extends Component {
constructor(props) {
super(props);
this.state = {
theme: 'light',
};
}
componentDidMount() {
const darkmode = document.querySelector('html').classList.contains('dark');
if (darkmode) {
this.setState({ theme: 'dark' });
} else {
this.setState({ theme: 'light' });
}
}
render() {
return <ReCAPTCHA theme={this.state.theme} />;
}
}
ReCAPTCHA theme changes on hard refresh but not when function toggle is clicked which changes everything else using :root
selector when finding 'dark' appended to html.