mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
449 B
TypeScript
23 lines
449 B
TypeScript
import React from 'react';
|
|
|
|
interface CreateContextFactoryOptions<Props> {
|
|
defaultValue: Props;
|
|
displayName: string;
|
|
}
|
|
|
|
export function createContextFactory<Props>(
|
|
options: CreateContextFactoryOptions<Props>
|
|
) {
|
|
const Context = React.createContext(options.defaultValue);
|
|
Context.displayName = options.displayName;
|
|
|
|
function useContext(): Props {
|
|
return React.useContext(Context);
|
|
}
|
|
|
|
return {
|
|
Context,
|
|
useContext,
|
|
};
|
|
}
|