@rodw95/react-create-safe-context
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

react-create-safe-context

Helper to create context in TypeScript with no defaultValue, yet no need to check for undefined.

https://github.com/typescript-cheatsheets/react-typescript-cheatsheet#context

Example

import createSafeContext from '@rodw95/react-create-safe-context';

type AuthContextValue = {
  state: 'signedIn' | 'signedOut',
};

const AuthContext = createSafeContext<AuthContextValue>();  
export const useAuthContext = Context.hook;

export default ({ children }) => (
  <AuthContext.Provider value={{ state: 'signedIn' }}>
    {children}
  </AuthContext.Provider>
);

Package Sidebar

Install

npm i @rodw95/react-create-safe-context

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

3.49 kB

Total Files

5

Last publish

Collaborators

  • robbieopdeweegh