https://github.com/cool-hooks/react-safe-context-hooks
☔ Make sure the context exists
https://github.com/cool-hooks/react-safe-context-hooks
api confidence context error exists handler hooks react
Last synced: about 2 months ago
JSON representation
☔ Make sure the context exists
- Host: GitHub
- URL: https://github.com/cool-hooks/react-safe-context-hooks
- Owner: cool-hooks
- License: mit
- Created: 2020-09-24T14:58:10.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-18T23:50:19.000Z (almost 2 years ago)
- Last Synced: 2025-03-18T13:21:19.444Z (2 months ago)
- Topics: api, confidence, context, error, exists, handler, hooks, react
- Language: TypeScript
- Homepage: https://codesandbox.io/s/react-safe-context-hooks-6me74
- Size: 647 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# [react-safe-context-hooks](https://github.com/cool-hooks/react-safe-context-hooks)
[](https://www.npmjs.com/package/react-safe-context-hooks)
[](https://www.npmjs.com/package/react-safe-context-hooks)
[](https://www.npmjs.com/package/react-safe-context-hooks)
[](https://codecov.io/gh/cool-hooks/react-safe-context-hooks)
[](https://app.travis-ci.com/github/cool-hooks/react-safe-context-hooks)
[](https://bundlephobia.com/result?p=react-safe-context-hooks)## About
Make sure the context exists
### Demo
**[Playground – play with the library in CodeSandbox](https://codesandbox.io/s/react-safe-context-hooks-6me74)**
### Similar Projects
- [react-safe-context](https://github.com/dslane/react-safe-context) by [dslane](https://github.com/dslane)
## How to Install
First, install the library in your project by npm:
```sh
$ npm install react-safe-context-hooks
```Or Yarn:
```sh
$ yarn add react-safe-context-hooks
```## Getting Started
**• Import a hook in a React application file:**
```js
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
```js
// before
const App = () => {
const context = useContext(ExampleContext);return
{JSON.stringify(context)};
};// after
ExampleContext.displayName = 'ExampleContext';const App = () => {
const context = useSafeContext(ExampleContext);return
{JSON.stringify(context)};
};
```## License
This project is licensed under the MIT License © 2020-present Jakub Biesiada