Reactにおけるコンポーネントの高度なスタイリング
Reactコンポーネント
Blockがあり、その中でStyled Componentsを使用して
スタイルを適用した
ButtonコンポーネントとContainerコンポーネントを作成したとします:
const Container = styled.div`
display: flex;
flex-direction: column;
width: 150px;
`;
const Button = styled.input`
background-color: orange;
font-size: 18px;
margin: 5px;
`;
ここで、別の
Buttonコンポーネントが必要になったとします。
ただし、ボタンのテキストは白で背景は緑色です。
このためには、ベースとなる
Buttonコンポーネントを基に新しい
MdButtonコンポーネントを作成し、
変更したいプロパティだけを指定すれば十分です:
const MdButton = styled(Button)`
color: white;
background-color: green;
`;
すべてのスタイル付きコンポーネントを
Blockに配置しましょう:
function Block() {
return (
<Container>
<Button>btn0</Button>
<MdButton>btn1</MdButton>
</Container>
);
}
空のReactコンポーネントBlock1を作成してください。
Styled Componentsを使用して、
幅と高さが150px、背景色が黄色、
境界線の幅が2pxのスタイル付きdivを作成し、
DIVAと名付けてください。
前のタスクで作成したdivを基に、
同じdivDIVBを作成してください。ただし、
背景色を緑、境界線の幅を3pxにします。
Block1内にdivContainerを作成してください。
その中に、前のタスクで作成した
2つのコンポーネントDIVAと、
それらの間に1つのDIVBコンポーネントを配置してください。