mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-12-05 05:29:01 +08:00
23 lines
609 B
TypeScript
23 lines
609 B
TypeScript
|
import { inject, provide, reactive, watchEffect } from 'vue';
|
||
|
|
||
|
function createContext<T extends Record<string, any>>(defaultValue?: T) {
|
||
|
const contextKey = Symbol('contextKey');
|
||
|
const useProvide = (props: T, newProps?: T) => {
|
||
|
const mergedProps = reactive<T>({} as T);
|
||
|
provide(contextKey, mergedProps);
|
||
|
watchEffect(() => {
|
||
|
Object.assign(mergedProps, props, newProps || {});
|
||
|
});
|
||
|
return mergedProps;
|
||
|
};
|
||
|
const useInject = () => {
|
||
|
return inject(contextKey, defaultValue as T) || ({} as T);
|
||
|
};
|
||
|
return {
|
||
|
useProvide,
|
||
|
useInject,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export default createContext;
|