Skip to content

Instantly share code, notes, and snippets.

@jasmo2
Last active March 10, 2025 17:11
Show Gist options
  • Save jasmo2/ac74666e76452e24e9cf552444227b88 to your computer and use it in GitHub Desktop.
Save jasmo2/ac74666e76452e24e9cf552444227b88 to your computer and use it in GitHub Desktop.
// Example auth Context: https://dev.to/shareef/context-api-with-typescript-and-next-js-2m25
import React, { createContext, useContext, useEffect, useState } from 'react';
interface ElementContextType {
prop1?: string | any
}
export const ElementContext = createContext<ElementContextType>({
prop1: 'default',
});
export const useElementContext = () => useContext(ElementContext)
type Props = {
children: React.ReactNode;
};
export const ElementProvider = (props: Props): JSX.Element => {
const [propState, setPropState] = useState<string | any>();
/**
* use useEffect if require
*/
return (
<ElementContext.Provider value={{ propState }}>
{props.children}
</ElementContext.Provider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment