@adamldoyle/use-provided-context
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

use-provided-context

Hook for accessing context while enforcing that the context was provided.

Features

  1. Allows providing an undefined default value for context but requiring context is defined when you go to use it (by being provided from a Provider)
  2. Typescript safe - the response type won't include undefined so you don't need to check for it
  3. Throws an error when a context is used without being provided (to be handled by whatever error handling you have in place)

Installation

  1. yarn add @adamldoyle/use-provided-context

Examples

OR

  • yarn storybook

OR

interface IStoryContext {
  value: string;
}

// Context is initialized as undefined and only given a value via some defined provider
const StoryContext = createContext<IStoryContext | undefined>(undefined);
StoryContext.displayName = 'StoryContext';

const StoryComponent: FC = () => {
  // StoryContext is guaranteed to provide IStoryContext in a typesafe way, an undefined context will throw an error
  const { value } = useProvidedContext(StoryContext);

  return <>Value from context: {value}</>;
};

Development

  1. yarn install
  2. yarn build

Contributors

Adam Doyle

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @adamldoyle/use-provided-context

Weekly Downloads

52

Version

1.1.0

License

MIT

Unpacked Size

6.22 kB

Total Files

7

Last publish

Collaborators

  • adamldoyle