小编use*_*457的帖子

在 React + Typescript 中铸造样式组件

我正在尝试在 React + Typescript 中实现动画。

interface IImageProps {
    frame: number
    width: number
    src: string
    onLoad: () => void
}

const Image = styled.img`
    transform: ${(props: IImageProps) => css`translate(0, -${props.frame * props.width}px)`};
`
Run Code Online (Sandbox Code Playgroud)

这会在控制台中引发警告:

styled-components.browser.esm.js:1507 Over 200 classes were generated for component styled.img. 
Consider using the attrs method, together with a style object for frequently changed styles.
Run Code Online (Sandbox Code Playgroud)

所以我试图使用attrs

const Image = styled.img.attrs((props: IImageProps) => ({
    style: { transform: `translate(0, -${props.frame * props.width}px)` },
}))``
Run Code Online (Sandbox Code Playgroud)

现在 TS 抱怨说:

Type '{ …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs styled-components

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

标签 统计

reactjs ×1

styled-components ×1

typescript ×1