This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
react-rest-request/src/request-context.tsx

31 lines
832 B
TypeScript
Raw Normal View History

import React from 'react';
import invariant from 'tiny-invariant';
import { Client } from './client';
export type RequestContextData = Readonly<{
client: Client;
defaultHeaders?: Record<string, string>;
}>
const RequestContext = React.createContext<RequestContextData | null>(null);
export type RequestProviderProps = Readonly<React.PropsWithChildren<RequestContextData>>
export function RequestProvider({ client, defaultHeaders, children }: RequestProviderProps) {
return (
<RequestContext.Provider value={{ client, defaultHeaders }}>
{children}
</RequestContext.Provider>
);
}
export function useRequestContext() {
const context = React.useContext(RequestContext);
invariant(context, 'useRequestContext() must be a child of <RequestProvider />');
return context;
}