(react) Styled Component

Sunghaeยท2020๋…„ 3์›” 15์ผ
0

๐Ÿ‘€ styled-component๋ž€?
cssํŒŒ์ผ์—†์ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ์•ˆ์—์„œ css์ž‘์—…์„ ํ•˜๋Š” ์Šคํƒ€์ผ ๊ธฐ๋ฒ•์œผ๋กœ CSS in JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์„ค์น˜
npm install --save styled-components

Styled Icon : react-icon๊ฐ™์ด styled-component ์ „์šฉ์œผ๋กœ ๋‚˜์˜จ ์•„์ด์ฝ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
npm install styled-icons --save

๐Ÿค” ์‚ฌ์šฉ๋ฒ•

import React, { Component } from "react";
import styled from "styled-components";

export default class App extends Component {
  render() {
   return(
      <SignUp>
        <Container>Hello</Container>
      </Signup>
    );
  }
}

const SignUp = styled.div`
  width: 100vw;
  height: 100vh;
  background-color: #202020;
`;

const Container = styled.div`
  width: 315px;
  margin: 0 auto;
  color: #fff;
  font-family: "Sofia Pro";
`;

'styled-component'๋ฅผ importํ•ด์ค€ ๋’ค, ์ง€์ •ํ•œ ๋ณ€์ˆ˜(SignUp, Container)์— ์›ํ•˜๋Š” ํƒœ๊ทธ(div ๋“ฑ๋“ฑ)์„ style๊ณผ ํ•จ๊ป˜ ๋‹ด์•„์ค€๋‹ค.


  • props ์ง€์ •
<Button CREATE PASSPORT</Button>
<Button account>USE POTTERMORE ACCOUNT</Button>
  
  const Button = styled.button`
  width: 280px;
  padding: 18px 31px;
  border-radius: 30px;

  color: ${props => props.account ? "#ffffff"}
`;

'account'๋ผ๋Š” props๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜์˜ ๋ฒ„ํŠผ์—๋งŒ css๋ฅผ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ props๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ƒ๋‹จ์— styled-component๋ฅผ importํ•œ ๋ถ€๋ถ„์— css ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

import styled, {css} from "styled-components";

 ${props =>
    props.account &&
    css`
      margin-left: 10px;
      color: #ffffff;
      background-color: transparent;
    `}

  • state๊ฐ’์„ props ๋„˜๊ธฐ๊ธฐ
this.state = {
  button: false
}

<Button button={this.state.button} onClick={() => 
  {button && this.handleClick();}}
  >
  CONFITM 
</Button>

0๊ฐœ์˜ ๋Œ“๊ธ€