1
|
import { useEffect } from 'react'
|
2
|
import { useDispatch, useSelector } from 'react-redux'
|
3
|
import { Navigate } from 'react-router-dom'
|
4
|
import { RootState } from '../redux/store'
|
5
|
import { logout } from './userSlice'
|
6
|
|
7
|
// Component that logs the user out if they are logged in
|
8
|
const Logout = () => {
|
9
|
const userLoggedIn = useSelector(
|
10
|
(state: RootState) => state.user.isLoggedIn
|
11
|
)
|
12
|
|
13
|
const dispatch = useDispatch()
|
14
|
|
15
|
// Check whether the user is logged in and if so log them out
|
16
|
useEffect(() => {
|
17
|
if (userLoggedIn) {
|
18
|
dispatch(logout())
|
19
|
}
|
20
|
}, [dispatch, userLoggedIn])
|
21
|
|
22
|
return <Navigate to="/" />
|
23
|
}
|
24
|
|
25
|
export default Logout
|