GitXplorerGitXplorer
r

react-create-safe-context

public
0 stars
0 forks
6 issues

Commits

List of commits on branch master.
Unverified
21bc8aa2f66bfa9d189bcc24dba9603d7b84d751

Allow default value

rrodw1995 committed 5 years ago
Unverified
8ef745e2ac355663d2957899b85c86e96df3bb53

Initial files

rrodw1995 committed 5 years ago
Verified
854ff361ed5710a91d410f91ba95ac9eb5e1da4c

Initial commit

rrodw1995 committed 5 years ago

README

The README file for this repository.

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>
);