From d7d493eaaa92a9a10a2996721e3d9f74dfa968f4 Mon Sep 17 00:00:00 2001 From: JiaQi <112228030+Yuiai01@users.noreply.github.com> Date: Fri, 25 Aug 2023 09:37:32 +0800 Subject: [PATCH] refactor(notification): reduce the configuration parameter code (#44372) * refactor(notification): reduce the configuration parameter code * chore: modify the return type definition * chore: delete the initialization function --- components/message/index.tsx | 24 +++++-------------- components/notification/index.tsx | 40 +++++++------------------------ 2 files changed, 14 insertions(+), 50 deletions(-) diff --git a/components/message/index.tsx b/components/message/index.tsx index a9c5e13457..7d2f1fdf20 100755 --- a/components/message/index.tsx +++ b/components/message/index.tsx @@ -1,9 +1,9 @@ 'use client'; -import { render } from 'rc-util/lib/React/render'; import * as React from 'react'; +import { render } from 'rc-util/lib/React/render'; + import ConfigProvider, { globalConfig, warnContext } from '../config-provider'; -import PurePanel from './PurePanel'; import type { ArgsProps, ConfigOptions, @@ -12,6 +12,7 @@ import type { NoticeType, TypeOpen, } from './interface'; +import PurePanel from './PurePanel'; import useMessage, { useInternalMessage } from './useMessage'; import { wrapPromiseFn } from './util'; @@ -70,7 +71,7 @@ function getGlobalContext() { return { prefixCls: mergedPrefixCls, - container: mergedContainer, + getContainer: () => mergedContainer!, duration, rtl, maxCount, @@ -84,20 +85,7 @@ interface GlobalHolderRef { } const GlobalHolder = React.forwardRef((_, ref) => { - const initializeMessageConfig: () => ConfigOptions = () => { - const { prefixCls, container, maxCount, duration, rtl, top } = getGlobalContext(); - - return { - prefixCls, - getContainer: () => container!, - maxCount, - duration, - rtl, - top, - }; - }; - - const [messageConfig, setMessageConfig] = React.useState(initializeMessageConfig); + const [messageConfig, setMessageConfig] = React.useState(getGlobalContext); const [api, holder] = useInternalMessage(messageConfig); @@ -107,7 +95,7 @@ const GlobalHolder = React.forwardRef((_, ref) => { const theme = global.getTheme(); const sync = () => { - setMessageConfig(initializeMessageConfig); + setMessageConfig(getGlobalContext); }; React.useEffect(sync, []); diff --git a/components/notification/index.tsx b/components/notification/index.tsx index dd852fea56..21c70ba697 100755 --- a/components/notification/index.tsx +++ b/components/notification/index.tsx @@ -1,10 +1,11 @@ 'use client'; -import { render } from 'rc-util/lib/React/render'; import * as React from 'react'; +import { render } from 'rc-util/lib/React/render'; + import ConfigProvider, { globalConfig, warnContext } from '../config-provider'; -import PurePanel from './PurePanel'; import type { ArgsProps, GlobalConfigProps, NotificationInstance } from './interface'; +import PurePanel from './PurePanel'; import useNotification, { useInternalNotification } from './useNotification'; let notification: GlobalNotification | null = null; @@ -45,7 +46,7 @@ function getGlobalContext() { return { prefixCls: mergedPrefixCls, - container: mergedContainer, + getContainer: () => mergedContainer!, rtl, maxCount, top, @@ -59,21 +60,10 @@ interface GlobalHolderRef { } const GlobalHolder = React.forwardRef((_, ref) => { - const [prefixCls, setPrefixCls] = React.useState(); - const [container, setContainer] = React.useState(); - const [maxCount, setMaxCount] = React.useState(); - const [rtl, setRTL] = React.useState(); - const [top, setTop] = React.useState(); - const [bottom, setBottom] = React.useState(); + const [notificationConfig, setNotificationConfig] = + React.useState(getGlobalContext); - const [api, holder] = useInternalNotification({ - prefixCls, - getContainer: () => container!, - maxCount, - rtl, - top, - bottom, - }); + const [api, holder] = useInternalNotification(notificationConfig); const global = globalConfig(); const rootPrefixCls = global.getRootPrefixCls(); @@ -81,21 +71,7 @@ const GlobalHolder = React.forwardRef((_, ref) => { const theme = global.getTheme(); const sync = () => { - const { - prefixCls: nextGlobalPrefixCls, - container: nextGlobalContainer, - maxCount: nextGlobalMaxCount, - rtl: nextGlobalRTL, - top: nextTop, - bottom: nextBottom, - } = getGlobalContext(); - - setPrefixCls(nextGlobalPrefixCls); - setContainer(nextGlobalContainer); - setMaxCount(nextGlobalMaxCount); - setRTL(nextGlobalRTL); - setTop(nextTop); - setBottom(nextBottom); + setNotificationConfig(getGlobalContext); }; React.useEffect(sync, []);