尝试扩展 Material-ui Button 组件以添加新道具。
目的是添加一个新道具:fontSize它有三个选项 - small、medium、large。
Run Code Online (Sandbox Code Playgroud)<Button variant="outlined" color="primary" fontSize="small"> button_small </Button>
并在 css 中使用它来进行所需的更改。
根据typescript 主题定制的材料 ui 文档,我已经定制了主题并且工作正常。
唯一的问题是尝试更新 Button 的道具类型不起作用。
我收到此错误是因为没有重载匹配,这很明显,因为 Material ui Button 组件不知道“fontSize”新道具。
错误 TS2769:没有与此调用匹配的重载。
重载 1 of 3, '(props: { href: string; } & { children?: ReactNode; color?: Color | undefined; disabled?: boolean | undefined; disableElevation?: boolean | undefined; disableFocusRipple?: boolean | undefined; ... 5 更多...; 变体?:“文本” | ... 2 更多... | 未定义; } & …
是否可以将某个对象的某些键拆包到新对象?
比方说,我想按键(3个拷贝a,b,c从)test对象到一个新的对象(abc)。下面提到的代码将起作用。
const test = {a:1, b:2, c:3, d:4, e:5 };
const {a, b, c} = test;
const abc = { a, b, c, f: 6};
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在一个语句中完成?
还有另一种方法。
const test = {a:1, b:2, c:3, d:4, e:5 };
const abc = { ...test, f: 6};
Run Code Online (Sandbox Code Playgroud)
但是,使用这种方法,我后来不得不删除不需要的键(d,e在我的情况)。
(最佳情况解决方案:如果我们不必跟踪不需要的密钥。可以有n个不需要的密钥。)