Obake Engineer Blog

TypeScript環境でEmotionのスタイル指定に任意のpropsを渡す方法

EmotionTypeScriptReact

2022/08/14

渡すpropsの型を指定しないとエラーになってしまう。

やり方

これではエラーになるので、

import styled from '@emotion/styled'

const H1 = styled.h1`
  padding-top: ${(props) => props.someNumber};
`;

const Component = () => <H1 someNumber={16}>hoge</H1>;

ジェネリクスを使って型を指定する

import styled from '@emotion/styled'

type HeadingProps = {
  someNumber: number;
};

const H1 = styled.h1<HeadingProps>`
  padding-top: ${(props) => props.someNumber};
`;

const Component = () => <H1 someNumber={16}>hoge</H1>;

参考

  • reactjs - How to pass props to a styled component in emotion? Using TypeScript - Stack Overflow
  • Emotion – Styled Components

/post-14

TypeScript環境でEmotionのスタイル指定に任意のpropsを渡す方法

Related Posts

  • 【JavaScript】typeofの違い【TypeScript】
  • 「create-react-app」+ 「TypeScript」環境で絶対パスのaliasを指定する
  • Reactのレンダリングメモ
  • Reactのレンダリングプロセス

Search

Recent Posts

  • Apollo Clientでリクエストヘッダーに認証トークンを付与する方法
  • 【Zsh】コマンド履歴にエラーの履歴を残さない設定
  • 【Laravel】PHPUnit実行時に開発用DBを初期化してしまわないために
  • TypeScript環境でEmotionのスタイル指定に任意のpropsを渡す方法
  • Reactのレンダリングメモ

All Tags

JavaScript (7)
React (4)
GatsbyJS (3)
TypeScript (3)
Zsh (2)
Webpack (2)
Git (1)
CSS (1)
Dotenv (1)
Redux (1)
Emotion (1)
Laravel (1)
PHP (1)
GraphQL (1)
Apollo (1)
obake
Obake Engineer Blog

の検索結果: 0件中 0件表示