【发布时间】:2019-06-27 19:44:08
【问题描述】:
使用 typescript 并希望使用 styled-components 为 Material UI 组件设置样式。
但是 StyledComponent 出现类型错误,显示 Type '{ children: string; }' is missing the following properties
import React, { PureComponent } from 'react';
import styled from 'styled-components'; // ^4.1.3
import { Button } from '@material-ui/core'; // ^3.9.1
class TestForm extends PureComponent {
render() {
return (
<>
<Button style={{ backgroundColor: 'red' }}>Work</Button>{/* OK */}
<StyledButton>Doesn't work</StyledButton>{/* Type Error happens here <=============== */}
{/**
Type '{ children: string; }' is missing the following properties from type 'Pick<Pick<(ButtonProps & RefAttributes<Component<ButtonProps, any, any>>) | (ButtonProps & { children?: ReactNode; }), "form" | "style" | "title" | "disabled" | "mini" | ... 279 more ... | "variant"> & Partial<...>, "form" | ... 283 more ... | "variant">': style, classes, className, innerRef [2739]
*/}
</>
);
}
}
const StyledButton = styled(Button)`
background: blue;
`;
export default TestForm;
显示儿童道具丢失。
我也尝试了以下方法,但仍然无法正常工作。
const StyledButton = styled(Button)<{ children: string; }>`
background: blue;
`;
有人知道如何在 typescript 中使用 Material UI 和 styled-components 吗?
【问题讨论】:
标签: reactjs typescript material-ui styled-components