@ilz5753/usectx

2.0.3 • Public • Published

@ilz5753/usectx

@ilz5753/usectx is a small, customizable TypeScript library that provides a React hook (useCtx) for retrieving context values and throwing errors when the retrieved value is undefined.

Installation

You can install the package using npm:

npm install @ilz5753/usectx

Or with yarn:

yarn add @ilz5753/usectx

Usage

import useCtx from "@ilz5753/usectx";

import MyContext from "./MyContext";

const Component = () => {
  const value = useCtx(MyContext, "Context value is undefined");

  // Use the value in your component
};

Parameters

  • ctx: The React context from which you want to retrieve the value.

  • errorMsg: A custom error message to be thrown if the context value is undefined.

Return Value

The retrieved value from the context. If the value is undefined, an error will be thrown with the specified error message.

Note

This hook should be used within a React component. It relies on the React useContext hook to retrieve the context value.

Readme

Keywords

none

Package Sidebar

Install

npm i @ilz5753/usectx

Weekly Downloads

0

Version

2.0.3

License

MIT

Unpacked Size

2.61 kB

Total Files

5

Last publish

Collaborators

  • ilz5753