react-safe-context-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

NPM version NPM downloads NPM license Codecov Travis Bundle size

About

Make sure context exists

Demo

Playground – play with library in CodeSandbox

Similar Projects

How to Install

First, install the library in your project by npm:

$ npm install react-safe-context-hooks

Or Yarn:

$ yarn add react-safe-context-hooks

Getting Started

• Import hook in React application file:

import { useSafeContext } from 'react-safe-context-hooks';

If you want to display context name in error message you need to add displayName to Context.

Example

// before
const App = () => {
  const context = useContext(ExampleContext);

  return <pre>{JSON.stringify(context)}</pre>;
};

// after
ExampleContext.displayName = 'ExampleContext';

const App = () => {
  const context = useSafeContext(ExampleContext);

  return <pre>{JSON.stringify(context)}</pre>;
};

License

This project is licensed under the MIT License © 2020-present Jakub Biesiada

Package Sidebar

Install

npm i react-safe-context-hooks

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

6.68 kB

Total Files

10

Last publish

Collaborators

  • jbiesiada