Styled-Componentを使いまわす
Rails × react SPAの作成に当たりstyled-componentを使用している。
せっかくなのでstyled-componentを使いまわす練習がしたい(0からスタイル考えるのめんどくさいってのは内緒)
①コンポーネントをexport
styled-componentを定義している箇所でexportしましょう
export const FormBlock = styled.div `
margin: 10px auto;
width: 40%;
display: flex;
flex-direction: column;
②コンポーネントのimport
コンポーネントを使いまわす先でimportします。
このとき通常の名前付きexportの場合は{}で囲う必要があります。
default exportの場合は囲わなくていいみたいです。
//コンポーネントの読み込み
import {FormBlock} from "../common/UserModal.jsx"
今回は通常のexportなので{}で囲います。
③スタイルを継承しつつアレンジ
スタイルを継承しつつアレンジすることもできます。
その場合はexport先となるコンポーネントでスタイルを定義する際に
以下のように styled(継承したいコンポーネント名) と記述します。
const BooksFormBlock = styled(FormBlock)`
width: 80%;
& label {
font-size: 16px;
}
& input {
height: 24px;
line-height: 24px;
}
& textarea {
height: 100px;
resize: none;
}
`
こうするとexportしたコンポーネントのスタイルを踏まえつつ、アレンジしたいところだけ
アレンジできます。
実際にやってみました。
Author And Source
この問題について(Styled-Componentを使いまわす), 我々は、より多くの情報をここで見つけました https://qiita.com/togo_mentor/items/96f859f9d4e9a86d2f7e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .