小编Rah*_*dhi的帖子

在打字稿中扩展 material-ui 组件

尝试扩展 Material-ui Button 组件以添加新道具。

目的是添加一个新道具:fontSize它有三个选项 - smallmediumlarge

<Button variant="outlined" color="primary" fontSize="small">
    button_small
</Button>
Run Code Online (Sandbox Code Playgroud)

并在 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 更多... | 未定义; } & …

typescript reactjs material-ui

7
推荐指数
1
解决办法
704
查看次数

ES6-解构分配-将某些属性从现有对象解压缩到新对象?

是否可以将某个对象的某些键拆包到新对象?

比方说,我想按键(3个拷贝abc从)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)

但是,使用这种方法,我后来不得不删除不需要的键(de在我的情况)。

(最佳情况解决方案:如果我们不必跟踪不需要的密钥。可以有n个不需要的密钥。)

javascript object destructuring ecmascript-6

3
推荐指数
1
解决办法
639
查看次数