Skip to content

Commit

Permalink
Fix StrictMode in React 18 (#255)
Browse files Browse the repository at this point in the history
* chore(deps): bump react and styled-components

* chore: use createRoot, StrictMode in example

* fix: move timer to class field to support StrictMode

Fixes #254

* chore: fix tests with react 18

* chore: remove unused enzyme-adapter-react-16 types

Co-authored-by: Jakub Sarnowski <hello@jsarnowski.com>
  • Loading branch information
jgoz and jsardev authored Jun 26, 2022
1 parent f1d8ea3 commit 251fb14
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 186 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-tips-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'reaptcha': patch
---

support StrictMode in react@18
5 changes: 5 additions & 0 deletions .changeset/smart-books-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'reaptcha-example': minor
---

Use react@18 and enable StrictMode
8 changes: 4 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
},
"dependencies": {
"formik": "^1.1.1",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"reaptcha": "^1.11.0",
"styled-components": "4.1.3"
"react": "^18.1.0",
"react-dom": "^18.1.0",
"reaptcha": "^1.12.0",
"styled-components": "^5.3.5"
},
"devDependencies": {
"@types/styled-components": "^5.1.24",
Expand Down
14 changes: 12 additions & 2 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import React from 'react';
import { render } from 'react-dom';
import { createRoot } from 'react-dom/client';
import App from './containers';

render(<App />, document.getElementById('app'));
const app = document.getElementById('app');
if (!app) {
throw new Error('No app element found');
}

const root = createRoot(app);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
2 changes: 1 addition & 1 deletion lib/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import test from 'ava';
import sinon from 'sinon';
import jsdom from 'jsdom-global';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';

import Reaptcha, { Grecaptcha, Props } from './index';

Expand Down
9 changes: 4 additions & 5 deletions lib/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ type State = {
ready: boolean;
rendered: boolean;
invisible: boolean;
timer?: number;
};

const RECAPTCHA_SCRIPT_URL = 'https://recaptcha.net/recaptcha/api.js';
Expand All @@ -78,6 +77,7 @@ const PROPS_THAT_SHOULD_CAUSE_RERENDER: Array<keyof RecaptchaBaseConfig> = [

class Reaptcha extends Component<Props, State> {
container?: HTMLDivElement | null;
timer?: number | undefined;

state: State = {
instanceKey: Date.now(),
Expand Down Expand Up @@ -156,8 +156,8 @@ class Reaptcha extends Component<Props, State> {
_onError = () => this.props.onError && this.props.onError();

_stopTimer = (): void => {
if (this.state.timer) {
clearInterval(this.state.timer);
if (this.timer) {
clearInterval(this.timer);
}
};

Expand All @@ -167,13 +167,12 @@ class Reaptcha extends Component<Props, State> {
if (this._isAvailable()) {
this._prepare();
} else {
const timer = window.setInterval(() => {
this.timer = window.setInterval(() => {
if (this._isAvailable()) {
this._prepare();
this._stopTimer();
}
}, 500);
this.setState({ timer });
}
};

Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/enzyme": "^3.10.11",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jsdom-global": "^3.0.2",
"@types/node": "^17.0.21",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.13",
"@types/react": "^18.0.12",
"@types/react-dom": "^18.0.5",
"@types/sinon": "^10.0.11",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7",
"ava": "^4.0.1",
"c8": "^7.10.0",
"cz-conventional-changelog": "^3.1.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"eslint": "^8.10.0",
"eslint-plugin-react": "^7.29.2",
"husky": "^4.2.3",
Expand Down
Loading

0 comments on commit 251fb14

Please sign in to comment.