gan*_*ate 3 typescript reactjs material-ui styled-components
我是材料用户界面的新手。在这里,我试图创建一个样式化的组件,它将是一个Typography. 所以,我尝试的是,
import styled from 'styled-components';
import {
FormGroup,
FormControlLabel,
Tooltip,
FormControl,
Select,
Radio,
Typography,
Button
} from '@material-ui/core'
const StyledTypography = styled.Typography<Itest>(({ marginLeft, marginRight }) => ({
}))
Run Code Online (Sandbox Code Playgroud)
但这给了我一个编译时错误。
Property 'Typography' does not exist on type 'ThemedStyledInterface<ThemeInterface>'
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个 ?
我使用了以下方式
const StyledTypography = styled(Typography)<ISortBySelector>(({ fontSize }) => ({
&& {
fontFamily: 'Roboto',
fontSize: fontSize ? fontSize : '10px',
fontWeight: 'normal',
fontStretch: 'normal',
fontStyle: 'normal',
lineHeight: 'normal',
letterSpacing: fontSize ? '0.14px' : '0.07px',
width: fontSize ? '50px' : '35px',
height: fontSize ? '19px' : '14px',
color: '#000000',
cursor: 'pointer'
}
}))
Run Code Online (Sandbox Code Playgroud)
我不确定您是否使用了正确的语法,但这就是我通常将组件传递到样式组件的方式(注意我没有使用点表示法)。此外,您可以使用常用的 CSS 语法,而不是较低的驼峰格式。
import Typography from '@material-ui/core/Typography';
import styled from 'styled-components';
const StyledTypography = styled(Typography)<Itest>`
&& {
font-family: Roboto;
// customise your styles here
}
`;
Run Code Online (Sandbox Code Playgroud)
如果您希望将其他道具传递给StyledTypography,例如设置variant为caption,
<StyledTypography variant='caption'>
Some text
</StyledTypography>
Run Code Online (Sandbox Code Playgroud)
我不认为使用点表示法是你的问题。我认为这就是您想要实现的目标:
import { styled } from '@mui/material/styles'
import Typography from '@mui/material/Typography'
export const StyledTypography = styled(Typography)(({ theme }) => ({
'&': {
color: theme.palette.common.black,
// ...and so on
},
})) as typeof Typography
Run Code Online (Sandbox Code Playgroud)
如果您不使用主题中的变量,则另一个答案是可以的。另请注意,目前特定组件上的 prop 存在错误component,因此as typeof Typography可能需要。