Skip to content

Commit 51eef7b

Browse files
committed
Update useCallback
1 parent bbd1894 commit 51eef7b

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

frontend/src/components/auth/Login.tsx

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useCallback } from 'react'
22
import { Formik, FormikActions } from 'formik'
33

44
import TextField from '@material-ui/core/TextField'
@@ -47,30 +47,30 @@ const Login: React.FC<Props> = () => {
4747
const { history } = useReactRouter()
4848
const [error, setError] = useState<string>()
4949

50-
const handleSubmit = (
51-
values: FormValues,
52-
{ setSubmitting }: FormikActions<FormValues>,
53-
) => {
54-
setError(undefined)
55-
loginMutation({
56-
variables: {
57-
email: values.email,
58-
password: values.password,
59-
},
60-
})
61-
.then((res) => {
62-
if (!res.data) return
63-
// console.log('login success: ', res.data.login)
64-
65-
localStorage.setItem('token', res.data.login.token)
66-
history.push('/')
67-
})
68-
.catch((e) => {
69-
console.error(e)
70-
setError(e.message)
71-
setSubmitting(false)
50+
const handleSubmit = useCallback(
51+
(values: FormValues, { setSubmitting }: FormikActions<FormValues>) => {
52+
setError(undefined)
53+
loginMutation({
54+
variables: {
55+
email: values.email,
56+
password: values.password,
57+
},
7258
})
73-
}
59+
.then((res) => {
60+
if (!res.data) return
61+
// console.log('login success: ', res.data.login)
62+
63+
localStorage.setItem('token', res.data.login.token)
64+
history.push('/')
65+
})
66+
.catch((e) => {
67+
console.error(e)
68+
setError(e.message)
69+
setSubmitting(false)
70+
})
71+
},
72+
[history, loginMutation],
73+
)
7474

7575
return (
7676
<Box

frontend/src/components/auth/Signup.tsx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useCallback } from 'react'
22
import { Formik, FormikActions } from 'formik'
33

44
import TextField from '@material-ui/core/TextField'
@@ -52,31 +52,31 @@ const Signup: React.FC<Props> = () => {
5252
const { history } = useReactRouter()
5353
const [error, setError] = useState<string>()
5454

55-
const handleSubmit = (
56-
values: FormValues,
57-
{ setSubmitting }: FormikActions<FormValues>,
58-
) => {
59-
setError(undefined)
60-
signupMutation({
61-
variables: {
62-
name: values.name,
63-
email: values.email,
64-
password: values.password,
65-
},
66-
})
67-
.then((res) => {
68-
if (!res.data) throw Error()
69-
// console.log('signup success: ', res.data.login)
70-
71-
localStorage.setItem('token', res.data.signup.token)
72-
history.push('/')
73-
})
74-
.catch((e) => {
75-
console.error(e)
76-
setError(e.message)
77-
setSubmitting(false)
55+
const handleSubmit = useCallback(
56+
(values: FormValues, { setSubmitting }: FormikActions<FormValues>) => {
57+
setError(undefined)
58+
signupMutation({
59+
variables: {
60+
name: values.name,
61+
email: values.email,
62+
password: values.password,
63+
},
7864
})
79-
}
65+
.then((res) => {
66+
if (!res.data) throw Error()
67+
// console.log('signup success: ', res.data.login)
68+
69+
localStorage.setItem('token', res.data.signup.token)
70+
history.push('/')
71+
})
72+
.catch((e) => {
73+
console.error(e)
74+
setError(e.message)
75+
setSubmitting(false)
76+
})
77+
},
78+
[history, signupMutation],
79+
)
8080

8181
return (
8282
<Box

frontend/src/components/layout/Header.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useCallback } from 'react'
22

33
import { makeStyles, Theme } from '@material-ui/core/styles'
44
import IconButton from '@material-ui/core/IconButton'
@@ -56,24 +56,27 @@ const Header: React.FC<Props> = ({ isDrawerOpen, onDrawerOpen, isMobile }) => {
5656

5757
const { history } = useReactRouter()
5858

59-
const handleMenuOpen = (e: any) => {
60-
setMenuAnchorEl(e.currentTarget)
61-
}
59+
const handleMenuOpen = useCallback(
60+
(e: any) => {
61+
setMenuAnchorEl(e.currentTarget)
62+
},
63+
[setMenuAnchorEl],
64+
)
6265

63-
const handleMenuClose = () => {
66+
const handleMenuClose = useCallback(() => {
6467
setMenuAnchorEl(null)
65-
}
68+
}, [setMenuAnchorEl])
6669

67-
const handleLogout = () => {
70+
const handleLogout = useCallback(() => {
6871
handleMenuClose()
6972
localStorage.removeItem('token')
7073
history.push('/login')
71-
}
74+
}, [handleMenuClose, history])
7275

73-
const handleToProfile = () => {
76+
const handleToProfile = useCallback(() => {
7477
handleMenuClose()
7578
history.push('/profile')
76-
}
79+
}, [handleMenuClose, history])
7780

7881
return (
7982
<AppBar

0 commit comments

Comments
 (0)