Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React Context Provider
import React, { createContext, useContext, useState } from '~/session/react';
import Session from 'sessionModule';
import { resetSessionRequest } from 'session-client';
const SessionContext = createContext(Session.default());
SessionContext.displayName = 'Session';
function useSession(initialSession) {
const [session, setSession] = useState(initialSession);
const resetSession = async (applicationId) => {
const response = await resetSessionRequest(applicationId);
if (response.isFailure) {
throw new TypeError('Unable to reset session');
}
setSession(response);
}
return {
resetSession: resetSession,
session: session
}
}
export function SessionProvider({children, initialSession = Session.default()}) {
return (
<SessionContext.Provider value={useSession(initialSession)}>{children}</SessionContext.Provider>
);
}
export default {
useSessionContext: () => useContext(SessionContext)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment