优化
This commit is contained in:
22
client/shared/contexts/factory.tsx
Normal file
22
client/shared/contexts/factory.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user