What does didRequest do in this component?
const AuthInit: FC<WithChildren> = ({children}) => {
const {auth, logout, setCurrentUser} = useAuth()
const didRequest = useRef(false)
const [showSplashScreen, setShowSplashScreen] = useState(true)
// We should request user by authToken (IN OUR EXAMPLE IT'S API_TOKEN) before rendering the application
useEffect(() => {
const requestUser = async (apiToken: string) => {
try {
if (!didRequest.current) {
const {data} = await getUserByToken(apiToken)
if (data) {
setCurrentUser(data)
}
}
} catch (error) {
console.error(error)
if (!didRequest.current) {
logout()
}
} finally {
setShowSplashScreen(false)
} return () => (didRequest.current = true)
}
if (auth && auth.api_token) {
requestUser(auth.api_token)
} else {
logout()
setShowSplashScreen(false)
}
// eslint-disable-next-line
}, [])
return showSplashScreen ? <LayoutSplashScreen /> : <>{children}</>
}
I've got two questions:
1. What does didRequest
do in this component?
2. What sequences of clicks do I have to do in demo1 for didRequest to be set to true
- so that it breaks here if I set a breakpoint: return () => (didRequest.current = true)
?
Replies (1)
Hi,
1) didRequest
is needed to be sure that we made only one request the getting user data from the server.
2) Just refresh the page and you will have a request.
Regards,
Keenthemes support