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.
;;; const CounterContext = ; const Example1 = <CounterContextProvider value="1"> // Warning: Failed prop type: Invalid prop `value` of type `string` supplied to `PropTyped(Provider)`, expected `number`. ... </CounterContextProvider>;
It takes an optional default value for the context.
const CounterContext = ; const Example2 = <CounterContextProvider> ... <CounterContextConsumer> value</CounterContextConsumer> // 0 </CounterContextProvider>;
Thanks to the expressiveness of prop-types
, arbitrarily complex values are supported.
const ReadWriteThemeContext =