Skip to content

Commit 9922566

Browse files
committed
refactor: remove React import and replace React.FC
1 parent fcb6ff3 commit 9922566

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

src/google-recaptcha-provider.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import * as React from 'react';
2-
import { FC, useMemo, useState, useEffect, useCallback } from 'react';
1+
import {
2+
useMemo,
3+
useState,
4+
useEffect,
5+
useCallback,
6+
createContext,
7+
ReactNode
8+
} from 'react';
39
import { cleanGoogleRecaptcha, injectGoogleReCaptchaScript } from './utils';
410

511
enum GoogleRecaptchaError {
@@ -18,31 +24,32 @@ interface IGoogleReCaptchaProviderProps {
1824
appendTo?: 'head' | 'body';
1925
id?: string;
2026
};
27+
children: ReactNode;
2128
}
2229

2330
export interface IGoogleReCaptchaConsumerProps {
2431
executeRecaptcha?: (action?: string) => Promise<string>;
2532
}
2633

27-
const GoogleReCaptchaContext = React.createContext<IGoogleReCaptchaConsumerProps>(
28-
{
29-
executeRecaptcha: () => {
30-
// This default context function is not supposed to be called
31-
throw Error('GoogleReCaptcha Context has not yet been implemented');
32-
}
34+
const GoogleReCaptchaContext = createContext<IGoogleReCaptchaConsumerProps>({
35+
executeRecaptcha: () => {
36+
// This default context function is not supposed to be called
37+
throw Error(
38+
'GoogleReCaptcha Context has not yet been implemented, if you are using useGoogleReCaptcha hook, make sure the hook is called inside component wrapped by GoogleRecaptchaProvider'
39+
);
3340
}
34-
);
41+
});
3542

3643
const { Consumer: GoogleReCaptchaConsumer } = GoogleReCaptchaContext;
3744

38-
export const GoogleReCaptchaProvider: FC<IGoogleReCaptchaProviderProps> = ({
45+
export function GoogleReCaptchaProvider({
3946
reCaptchaKey,
4047
useEnterprise = false,
4148
useRecaptchaNet = false,
4249
scriptProps,
4350
language,
4451
children
45-
}) => {
52+
}: IGoogleReCaptchaProviderProps) {
4653
const [greCaptchaInstance, setGreCaptchaInstance] = useState<null | {
4754
execute: Function;
4855
}>(null);
@@ -115,6 +122,6 @@ export const GoogleReCaptchaProvider: FC<IGoogleReCaptchaProviderProps> = ({
115122
{children}
116123
</GoogleReCaptchaContext.Provider>
117124
);
118-
};
125+
}
119126

120127
export { GoogleReCaptchaConsumer, GoogleReCaptchaContext };

src/google-recaptcha.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { FC, useEffect } from 'react';
1+
import { useEffect } from 'react';
22
import { useGoogleReCaptcha } from './use-google-recaptcha';
33

44
export interface IGoogleRecaptchaProps {
55
onVerify: (token: string) => void | Promise<void>;
66
action?: string;
77
}
88

9-
export const GoogleReCaptcha: FC<IGoogleRecaptchaProps> = ({
10-
action,
11-
onVerify
12-
}) => {
9+
export function GoogleReCaptcha({ action, onVerify }: IGoogleRecaptchaProps) {
1310
const googleRecaptchaContextValue = useGoogleReCaptcha();
1411

1512
useEffect(() => {
@@ -35,4 +32,4 @@ export const GoogleReCaptcha: FC<IGoogleRecaptchaProps> = ({
3532
}, [action, onVerify, googleRecaptchaContextValue]);
3633

3734
return null;
38-
};
35+
}

src/use-google-recaptcha.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as React from 'react';
1+
import { useContext } from 'react';
22
import { GoogleReCaptchaContext } from './google-recaptcha-provider';
33

4-
export const useGoogleReCaptcha = () =>
5-
React.useContext(GoogleReCaptchaContext);
4+
export const useGoogleReCaptcha = () => useContext(GoogleReCaptchaContext);

src/with-google-recaptcha.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import hoistNonReactStatics from 'hoist-non-react-statics';
2-
import * as React from 'react';
2+
import { ComponentType } from 'react';
33
import {
44
GoogleReCaptchaConsumer,
55
IGoogleReCaptchaConsumerProps
@@ -10,22 +10,22 @@ export interface IWithGoogleReCaptchaProps {
1010
}
1111

1212
// tslint:disable-next-line:only-arrow-functions
13-
export const withGoogleReCaptcha = function<OwnProps>(
14-
Component: React.ComponentType<OwnProps & Partial<IWithGoogleReCaptchaProps>>
15-
): React.ComponentType<OwnProps & Partial<IWithGoogleReCaptchaProps>> {
16-
const WithGoogleReCaptchaComponent: React.FunctionComponent<
17-
OwnProps & Partial<IWithGoogleReCaptchaProps>
18-
> = props => (
13+
export const withGoogleReCaptcha = function <OwnProps>(
14+
Component: ComponentType<OwnProps & Partial<IWithGoogleReCaptchaProps>>
15+
): ComponentType<OwnProps & Partial<IWithGoogleReCaptchaProps>> {
16+
const WithGoogleReCaptchaComponent = (
17+
props: OwnProps & Partial<IWithGoogleReCaptchaProps>
18+
) => (
1919
<GoogleReCaptchaConsumer>
2020
{googleReCaptchaValues => (
2121
<Component {...props} googleReCaptchaProps={googleReCaptchaValues} />
2222
)}
2323
</GoogleReCaptchaConsumer>
2424
);
2525

26-
WithGoogleReCaptchaComponent.displayName = `withGoogleReCaptcha(${Component.displayName ||
27-
Component.name ||
28-
'Component'})`;
26+
WithGoogleReCaptchaComponent.displayName = `withGoogleReCaptcha(${
27+
Component.displayName || Component.name || 'Component'
28+
})`;
2929

3030
hoistNonReactStatics(WithGoogleReCaptchaComponent, Component);
3131

0 commit comments

Comments
 (0)