@ilz5753/usectx
v2.0.3
Published
`@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.
Downloads
4
Readme
@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.