Last active
April 12, 2022 06:41
-
-
Save ApolloTang/adb8571f97923fc6575ae24387c3bae2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
let resetClientSession: () => void; | |
const SessionCtx = React.createContext(null); | |
type propsType = { | |
children: JSX.Element; | |
}; | |
const ClientSessionProvide = (props: propsType) => { | |
const { children } = props; | |
let id; | |
const ctx = { isFullLogin: false }; // I am not sure if this should be a useState | |
useEffect(() => { | |
resetClientSession = () => { | |
if (id) { | |
clearTimeout(id); | |
} | |
ctx.isFullLogin = true; | |
id = setTimeout(() => { | |
ctx.isFullLogin = false; | |
}, fifteenMin ); | |
}; | |
}, []); | |
return ( | |
<SessionCtx.Provider value={ctx}> | |
<Foo>{children}</Foo> | |
</SessionCtx.Provider> | |
); | |
}; | |
export { | |
SessionCtx, | |
ClientSessionProvide, | |
resetClientSession, // <--- This is a live export see | |
// https://stackoverflow.com/questions/32558514/javascript-es6-export-const-vs-export-let | |
// it is assign the value after ClientSessionProvide has mounted. | |
}; | |
function Foo(props: propsType) { | |
const { children } = props; | |
const { isFullLogin } = React.useContext(SessionCtx); | |
return ( | |
<> | |
<div>{ isFullLogin }</div> | |
{children} | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment