cool-hooks / react-safe-context-hooks

☔ Make sure the context exists

Home Page:https://codesandbox.io/s/react-safe-context-hooks-6me74

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NPM version NPM downloads NPM license Codecov Travis Bundle size

About

Make sure the context exists

Demo

Playground – play with the 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 a hook in a React application file:

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

If you want to display the context name in the error message, you need to add displayName to the 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

About

☔ Make sure the context exists

https://codesandbox.io/s/react-safe-context-hooks-6me74

License:MIT License


Languages

Language:TypeScript 74.9%Language:JavaScript 23.8%Language:Shell 1.3%