mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-27 12:39:49 +08:00
1 line
7.7 KiB
JavaScript
1 line
7.7 KiB
JavaScript
(("undefined"!=typeof globalThis?globalThis:self).makoChunk_antd=("undefined"!=typeof globalThis?globalThis:self).makoChunk_antd||[]).push([["da19d117"],{da19d117:function(e,a,t){"use strict";t.d(a,"__esModule",{value:!0}),t.d(a,"texts",{enumerable:!0,get:function(){return n;}}),t("54107b4f");let n=[{value:"You may have received this warning when upgrading Ant Design:",paraId:0},{value:"Warning: [antd: XXX] `old prop` is deprecated. Please use `new prop` instead.\n",paraId:1},{value:"This is because antd has some historical debt in API design. For example, in antd v3 and before, the code of TreeSelect was directly copied from Select and extended on this basis. There are differences in their search styles:",paraId:2},{value:"And in the maintenance process, developers want to control the content of the search box. Unfortunately, this requirement was PR by different developers at different times. So two different properties were added, one called ",paraId:3},{value:"inputValue",paraId:3},{value:" and the other called ",paraId:3},{value:"searchValue",paraId:3},{value:":",paraId:3},{value:'// Select in combobox mode, the search box is the input box, `inputValue` looks reasonable\n<Select inputValue="search" />\n\n// TreeSelect\'s search box is in the popup layer, `searchValue` is also reasonable\n<TreeSelect searchValue="search" />\n',paraId:4},{value:"In multiple mode, the Select component will clear the search box content after selecting the content. But in some scenarios, developers want to keep it. So TreeSelect and Select added the ",paraId:5},{value:"autoClearSearchValue",paraId:5},{value:" property.",paraId:5},{value:"Wait! Select is called ",paraId:6},{value:"inputValue",paraId:6},{value:", why should it be called ",paraId:6},{value:"autoClearSearchValue",paraId:6},{value:"? Obviously it should be called ",paraId:6},{value:"autoClearInputValue",paraId:6},{value:". If we continue to grow other similar API styles on the existing API. You will find that the component's props are getting more and more split. This will also cause bad smells in code maintenance. For example, in the above example, we later extracted the Select component into a unified UI layer and merged it into the ",paraId:6},{value:"rc-select",paraId:6},{value:" component. ",paraId:6},{value:"rc-tree-select",paraId:6},{value:" only needs to implement the content of the popup layer, and the structure and style of the input box can be completely reused with Select. But because the APIs of the two are inconsistent, we need to do extra processing, so we need to refactor these API debts and unify them during the iteration process. (In v4, we merged it into ",paraId:6},{value:"searchValue",paraId:6},{value:" and unified the design)",paraId:6},{value:"However there is no silver bullet in the world, we can't design a perfect API at the beginning. Some APIs seem very reasonable at the beginning of the design, but as the iteration progresses, they will be found to be more or less inappropriate. For example, the popup layer was named dropdown in the early days, which corresponds to the popup content of Dropdown and Select components. But for Tooltip, dropdown is obviously not suitable. From a unified perspective, popup will be more suitable.",paraId:7},{value:"We gradually unified the API naming rules during the maintenance process (",paraId:8,tocIndex:0},{value:"API Naming rules",paraId:8,tocIndex:0},{value:"). When adding new features, we prefer to find similar APIs from existing ones. For existing APIs, deprecated warnings are gradually added. In order to maintain compatibility, our strategy is that the deprecated warnings provided by each version will continue to be compatible with a major version, and it will be removed in the next major version. For example, in v4, deprecated warnings are added, so they can still be used in v5, but they will be removed in v6. In this way, developers have enough time to migrate.",paraId:8,tocIndex:0},{value:"But from the developer's point of view, this is not reasonable. Developers only upgraded antd, but they have to suffer from the intrusion of console because of the API design mistakes of the component library. If a few usage warnings are mixed into the deprecated warnings, developers often have difficulty finding them. This situation is particularly prominent in major version upgrades. The business may not give you enough time to do the upgrade migration, so you have to use compatible packages and other technical means to make it run first. For long deprecated warnings, developers have to choose to temporarily (or permanently) ignore them. For this situation, usage warnings will be more important, so we propose the ",paraId:9,tocIndex:0},{value:"Warning Filter RFC",paraId:9,tocIndex:0},{value:".",paraId:9,tocIndex:0},{value:"You can aggregate the deprecated information through the ",paraId:10,tocIndex:1},{value:"warning",paraId:10,tocIndex:1},{value:" property of ConfigProvider:",paraId:10,tocIndex:1},{value:"<ConfigProvider warning={{ strict: false }} />\n",paraId:11,tocIndex:1},{value:"After aggregation, the original flattened deprecated information will be merged into an array and displayed in the console. And it will not affect the usage warnings:",paraId:12,tocIndex:1},{value:"As mentioned above, there is no silver bullet in API design. In order to prevent breaking change, we generally will not change the existing API implementation. But for some conventions, this will cause trouble. For example, the ",paraId:13,tocIndex:2},{value:"ref",paraId:13,tocIndex:2},{value:" component is a typical convention. As long as it is a React developer, you can understand that you can get the DOM node through ",paraId:13,tocIndex:2},{value:"ref",paraId:13,tocIndex:2},{value:" and do some basic operations such as ",paraId:13,tocIndex:2},{value:"focus",paraId:13,tocIndex:2},{value:". But for composite components, the calling method and DOM may not be unified. For example, the ",paraId:13,tocIndex:2},{value:"ref",paraId:13,tocIndex:2},{value:" of the Table component should obviously be the outermost div, but the ",paraId:13,tocIndex:2},{value:"scrollTo",paraId:13,tocIndex:2},{value:" method should correspond to the scroll container (if it is VirtualTable, it should be handled by the internal ",paraId:13,tocIndex:2},{value:"rc-virtual-list",paraId:13,tocIndex:2},{value:"). In antd mobile, ",paraId:13,tocIndex:2},{value:"ref",paraId:13,tocIndex:2},{value:" is designed as a composite structure, and the DOM node is always returned through ",paraId:13,tocIndex:2},{value:"nativeElement",paraId:13,tocIndex:2},{value:":",paraId:13,tocIndex:2},{value:"export interface SampleRef {\n nativeElement: HTMLElement;\n focus(): void;\n blur(): void;\n}\n",paraId:14,tocIndex:2},{value:"But in antd, because we did not make a convention for ",paraId:15,tocIndex:2},{value:"ref",paraId:15,tocIndex:2},{value:" early, we encountered difficulties in implementing the method. But fortunately, there is Proxy support, we can intercept ",paraId:15,tocIndex:2},{value:"ref",paraId:15,tocIndex:2},{value:" through Proxy and return the results we want:",paraId:15,tocIndex:2},{value:"useImperativeHandle(\n ref,\n () =>\n new Proxy(divRef.current, {\n get(target, key) {\n // ...\n },\n }),\n);\n",paraId:16,tocIndex:2},{value:"We can continue to be compatible with previous usage in this way. It is still a DOM node, but it also supports the definition call of SampleRef.",paraId:17,tocIndex:2},{value:"API design is a difficult problem. As the iteration of technology stack and components themselves. Some designs will gradually decay, and API upgrades themselves are also painful for developers. We hope that through this article, you can understand our design ideas and some problems in the upgrade process. If you have any suggestions or ideas, welcome to discuss on GitHub.",paraId:18,tocIndex:3}];}}]); |