import * as React from 'react' import { assert } from './assert' export const getReactVersion = () => { const [major, minor, patch] = React.version.split('.').map(Number) return { major, minor, patch } } type NonNullableValue = object | number | string | boolean | bigint | symbol type NonStrictContextValue = NonNullableValue | undefined type StrictContextValue = NonNullableValue | null /** * A function that makes it easy to use the React `createContext` function. * @param defaultValue The default value of the context. * @param providerName The name of the provider component. * **Required if `defaultValue` is `null`able. (not `undefined`!)** Used to make the error message human-readable if contextValue is `null`. */ export function createContext( defaultValue: ContextValue ): [React.Provider, () => ContextValue] export function createContext( defaultValue: ContextValue, providerName: string ): [ React.Provider, (consumerName: string) => NonNullable, ] export function createContext( defaultValue: ContextValue, providerName?: string ) { const Context = React.createContext(defaultValue) function useContext(consumerName?: any) { const contextValue = React.useContext(Context) assert( contextValue !== null, `'${consumerName}' must be used within '${providerName}'` ) return contextValue } return [Context.Provider, useContext] as const }