Skip to content

Commit 4cedfb5

Browse files
author
gcd
committed
convert class component to function component
1 parent 4bc8161 commit 4cedfb5

File tree

5 files changed

+245
-307
lines changed

5 files changed

+245
-307
lines changed

App.tsx

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {ReactElement, useEffect, useRef, useState} from 'react';
22
import {
33
StyleSheet,
44
View,
@@ -60,33 +60,25 @@ const styles = StyleSheet.create({
6060
},
6161
});
6262

63-
interface State {
64-
access_token?: string
65-
expires_in?: number
66-
refreshing: boolean
67-
localizedFirstName?: string
68-
message?: string
69-
}
70-
export default class AppContainer extends React.Component<{}, State> {
71-
state = {
72-
access_token: undefined,
73-
expires_in: undefined,
74-
refreshing: false,
75-
localizedFirstName: undefined,
76-
message: undefined,
77-
}
63+
export default function AppContainer() : ReactElement {
64+
const [accessToken, setAccessToken] = useState<string | undefined>();
65+
const [expiresIn, setExpiresIn] = useState<any | undefined>();
66+
const [refreshing, setRefreshing] = useState<boolean>(false);
67+
const [localizedFirstName, setLocalizedFirstName] = useState<string>();
68+
const [message, setMessage] = useState<string | undefined>();
69+
7870

79-
modal = React.createRef<LinkedInModal>()
71+
const modal = useRef<any>();
8072

81-
constructor(props: any) {
82-
super(props)
73+
useEffect(() => {
8374
StatusBar.setHidden(true)
84-
}
75+
}, []);
76+
8577

86-
getUser = async (data: LinkedInToken) => {
78+
const getUser = async (data: LinkedInToken) => {
8779
const {access_token, authentication_code} = data;
8880
if (!authentication_code) {
89-
this.setState({refreshing: true});
81+
setRefreshing(true);
9082

9183
const response = await fetch('https://api.linkedin.com/v2/me', {
9284
method: 'GET',
@@ -95,13 +87,17 @@ export default class AppContainer extends React.Component<{}, State> {
9587
},
9688
});
9789
const payload = await response.json();
98-
this.setState({...payload, refreshing: false});
90+
setAccessToken(payload.access_token);
91+
setExpiresIn(payload?.expires_in);
92+
setLocalizedFirstName(payload?.localizedFirstName);
93+
setMessage(payload?.message);
94+
setRefreshing(false);
9995
} else {
10096
alert(`authentication_code = ${authentication_code}`);
10197
}
10298
}
10399

104-
renderItem(label: string, value: string) {
100+
const renderItem = (label: string, value: string): ReactElement => {
105101
return value ? (
106102
<View style={styles.item}>
107103
<View style={styles.labelContainer}>
@@ -112,47 +108,44 @@ export default class AppContainer extends React.Component<{}, State> {
112108
<Text style={styles.value}>{value}</Text>
113109
</View>
114110
</View>
115-
) : null;
111+
) : <></>;
116112
}
117113

118-
signOut = () => {
119-
this.setState({refreshing: true});
120-
this.modal.current
121-
?.logoutAsync()
122-
.then(() =>
123-
this.setState({ localizedFirstName: undefined, refreshing: false }),
124-
);
114+
const signOut = () => {
115+
setRefreshing(true);
116+
modal.current?.logoutAsync()
117+
.then(() => {
118+
setLocalizedFirstName(undefined);
119+
setRefreshing(false);
120+
});
125121
}
126122

127-
render() {
128-
const {refreshing, localizedFirstName} = this.state;
129-
return (
130-
<View style={styles.container}>
131-
<View style={styles.linkedInContainer}>
132-
<LinkedInModal
133-
ref={this.modal}
134-
clientID={CLIENT_ID}
135-
clientSecret={CLIENT_SECRET}
136-
redirectUri={REDIRECT_URL}
137-
onSuccess={this.getUser}
138-
/>
139-
<Button
140-
title="Open from external"
141-
onPress={() => this.modal.current?.open()}
142-
/>
143-
</View>
123+
return (
124+
<View style={styles.container}>
125+
<View style={styles.linkedInContainer}>
126+
<LinkedInModal
127+
ref={modal}
128+
clientID={CLIENT_ID}
129+
clientSecret={CLIENT_SECRET}
130+
redirectUri={REDIRECT_URL}
131+
onSuccess={getUser}
132+
/>
133+
<Button
134+
title="Open from external"
135+
onPress={() => {modal.current?.open()}}
136+
/>
137+
</View>
144138

145-
{refreshing && <ActivityIndicator size="large" />}
139+
{refreshing && <ActivityIndicator size="large" />}
146140

147-
{localizedFirstName && (
148-
<>
149-
<View style={styles.userContainer}>
150-
{this.renderItem('Last name', localizedFirstName)}
151-
</View>
152-
<Button title="Log Out" onPress={this.signOut} />
153-
</>
154-
)}
155-
</View>
156-
);
157-
}
141+
{localizedFirstName && (
142+
<>
143+
<View style={styles.userContainer}>
144+
{renderItem('Last name', localizedFirstName)}
145+
</View>
146+
<Button title="Log Out" onPress={signOut} />
147+
</>
148+
)}
149+
</View>
150+
);
158151
}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gcou/react-native-linkedin",
3-
"version": "2.1.8",
3+
"version": "2.1.9",
44
"main": "lib/index.tsx",
55
"description": "React-Native LinkedIn, a simple LinkedIn login library for React-Native with WebView and Modal - compatibility with RN 0.69",
66
"repository": {
@@ -29,7 +29,6 @@
2929
"linkedin"
3030
],
3131
"dependencies": {
32-
"deprecated-react-native-prop-types": "^4.1.0",
3332
"expo-random": "^12.3.0",
3433
"query-string": "8.1.0",
3534
"ramda": "0.29.0",
@@ -39,11 +38,11 @@
3938
"devDependencies": {
4039
"@types/jest": "29.5.2",
4140
"@types/ramda": "^0.29.2",
42-
"@types/react": "~18.0.38",
41+
"@types/react": "~18.2.14",
4342
"@types/react-native": "~0.70.14",
4443
"@types/uuid": "^9.0.2",
4544
"babel-jest": "29.5.0",
46-
"babel-preset-expo": "^9.5.0",
45+
"babel-preset-expo": "^9.3.2",
4746
"babel-preset-react-native": "^4.0.1",
4847
"expo": "^46.0.21",
4948
"jest-expo": "^46.0.2",

0 commit comments

Comments
 (0)