Skip to content

Instantly share code, notes, and snippets.

@ApolloTang
Last active April 12, 2022 06:41
Show Gist options
  • Save ApolloTang/adb8571f97923fc6575ae24387c3bae2 to your computer and use it in GitHub Desktop.
Save ApolloTang/adb8571f97923fc6575ae24387c3bae2 to your computer and use it in GitHub Desktop.
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