Skip to content

feat(ConfigProvider): support global configuration #2921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025

Conversation

guoyunhe
Copy link
Contributor

@guoyunhe guoyunhe commented May 24, 2025

支持全局配置。

import { ConfigProvider, Line } from '@ant-design/plots';

<ConfigProvider common={{ theme: 'dark' }} line={{ legend: { color: { position: 'bottom' } } }}>
  <Line {...config}/>
</ConfigProvider>

@lxfu1
Copy link
Member

lxfu1 commented May 25, 2025

太强了

  1. 快速上手模块补充一下文档
  2. 添加个示例

@hustcc
Copy link
Collaborator

hustcc commented May 25, 2025

太强了

  1. 快速上手模块补充一下文档

  2. 添加个示例

review 下设计是否合理?

@guoyunhe guoyunhe force-pushed the config-provider branch 4 times, most recently from 9549bdb to 82150db Compare May 25, 2025 13:26
children?: ReactNode;
}

export default function ConfigProvider({ children, ...value }: ConfigProviderProps) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加上 Memo 避免不必要的二次渲染?

  const value = useMemo(() => props, [props]);
  
  return (
    <ConfigContext.Provider value={value}>
      {children}
    </ConfigContext.Provider>
  );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也可以合并配置之后在 makeChartComp 中实现。

import type { ViolinConfig } from './components/violin';
import type { WaterfallConfig } from './components/waterfall';
import type { WordCloudConfig } from './components/wordCloud';
import type { CommonConfig } from './interface';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

components/index.ts 有些重复,可以看下如何优化。

@lxfu1
Copy link
Member

lxfu1 commented May 26, 2025

太强了

  1. 快速上手模块补充一下文档
  2. 添加个示例

review 下设计是否合理?

设计上没什么问题,目前的一个问题是配置变更时所有组件会重新渲染,加上 Memo 就好了。

@guoyunhe
Copy link
Contributor Author

guoyunhe commented May 26, 2025

设计上没什么问题,目前的一个问题是配置变更时所有组件会重新渲染,加上 Memo 就好了。

@lxfu1useMemo(() => props, [props]) 应该不会生效,props 本身的引用每次渲染都不同,且 props 传入的对象也是每次渲染重新创建。如果要实现真正的 memo 的话就必须用 https://www.npmjs.com/package/react-fast-compare 之类的工具+魔改 useMemo(antd 有一个魔改版在 @rc-component/util 里提供),我个人觉得是有点过于复杂了,因为全局配置通常位于 React 应用的顶层,是极少变化的。

@lxfu1
Copy link
Member

lxfu1 commented May 26, 2025

设计上没什么问题,目前的一个问题是配置变更时所有组件会重新渲染,加上 Memo 就好了。

@lxfu1useMemo(() => props, [props]) 应该不会生效,props 本身的引用每次渲染都不同,且 props 传入的对象也是每次渲染重新创建。如果要实现真正的 memo 的话就必须用 https://www.npmjs.com/package/react-fast-compare 之类的工具,我个人觉得是有点过于复杂了,因为全局配置通常位于 React 应用的顶层,是极少变化的。

嗯嗯,那就先忽略,确实变化频率不高。

@lxfu1 lxfu1 merged commit 6a972a4 into ant-design:v2 May 26, 2025
5 of 6 checks passed
@lxfu1
Copy link
Member

lxfu1 commented May 26, 2025

@ant-design/[email protected]

@guoyunhe guoyunhe deleted the config-provider branch May 27, 2025 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants