New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@adamldoyle/use-provided-context

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

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.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
222
increased by43.23%
Maintainers
1
Weekly downloads
 
Created
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

Package last updated on 07 Nov 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc