LoginSignup
0
0

More than 3 years have passed since last update.

[小ネタ]styled-componentsをTypeScriptで利用する

Posted at

概要

styled-componentsをTypeScriptで利用する場合の記載方法について。

シンプルな書き方

親が特定のreact componentでないときは、下記のようにシンプルに記述する。

const Button = styled.button<{primary: boolean}>`
  color: ${({primary}) => primary ? 'skyblue' };
`

複雑な場合の書き方

親のpropsを参照する場合は、Pickを使う。

interface Props {
  primary
  secondary
  // 略
}

function MyComponent(props: Props) {
  return (
    <div>
      <Button secondary={props.secondary} primary={props.primary}>{props.children}</Button>
    </div>
  )
}

const Button = styled.button<Pick<Props, 'primary' | 'secondary'>>`
  ${({primary, secondary}) => primary ? css`` : secondary ? css`` : css``}
`
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0