Socket
Socket
Sign inDemoInstall

@adamldoyle/use-provided-context

Package Overview
Dependencies
5
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @adamldoyle/use-provided-context

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


Version published
Maintainers
1
Created

Readme

Source

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

  • https://adamldoyle-use-provided-context.netlify.app

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

FAQs

Last updated on 07 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc