An open API service indexing awesome lists of open source software.

https://github.com/mdibyo/react-typed-context


https://github.com/mdibyo/react-typed-context

Last synced: 12 months ago
JSON representation

Awesome Lists containing this project

README

          

# react-typed-context

Runtime type-checking for React Context Providers.

### Install

```
npm install --save react-typed-context
```

### Introduction

This package adds runtime type-checking of the value provided to React `Context.Provider`s by leveraging the `prop-types` package.

```js
import createContextOfType from 'react-typed-context';
import * as PropTypes from 'prop-types';
import * as React from 'react';

const CounterContext = createContextOfType(PropTypes.number.isRequired);

const Example1 = () => (
// Warning: Failed prop type: Invalid prop `value` of type `string` supplied to `PropTyped(Provider)`, expected `number`.
...

);
```

It takes an optional default value for the context.
```js
const CounterContext = createContextOfType(PropTypes.number.isRequired, 0);

const Example2 = () => (

...
{value => value} // 0

);
```

Thanks to the expressiveness of `prop-types`, arbitrarily complex values are supported.
```js
const ReadWriteThemeContext = createContextOfType(
PropTypes.shape({
theme: PropTypes.string,
setTheme: PropTypes.func.isRequired,
},
{ theme: 'dark' },
);
```