Post

🌌 React μž…λ¬Έ XVI - Styled-Component

🌌 React μž…λ¬Έ XVI - Styled-Component

πŸ“˜ γ€Žμ†Œν”Œμ˜ 처음 λ§Œλ‚œ λ¦¬μ•‘νŠΈγ€λ₯Ό 읽고 μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.

Styled-Component

1
npm install --save styled-components
  • CSS 문법을 κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜λ©΄μ„œ 결과물을 μŠ€νƒ€μΌ 적으둜 닀듬어진 Component ν˜•νƒœλ‘œ λ§Œλ“€μ–΄ μ£ΌλŠ” μ˜€ν”ˆμ†ŒμŠ€ λΌμ΄λΈŒλŸ¬λ¦¬λ‹€.
  • Component κ°œλ…μ„ μ‚¬μš©ν•˜λ―€λ‘œ React와 ꢁ합이 잘 λ§žλ‹€.

기본적인 μ‚¬μš©λ²•

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const name = '이름';
const region = '경기도';

function myTagFunction(strings, nameExp, regionExp) {
    let str0 = strings[0];  
    let str1 = strings[1];  
    let str2 = strings[2];  
		
    return `${str0}${nameExp}${str1}${regionExp}${str2}`;
}

const output = myTagFunction`제 이름은 ${name}이고, μ‚¬λŠ” 곳은 ${region}μž…λ‹ˆλ‹€.`

// 좜λ ₯ κ²°κ³Ό : 제 이름은 이름이고, μ‚¬λŠ” 곳은 κ²½κΈ°λ„μž…λ‹ˆλ‹€.
console.log(output)
  • Styled-ComponentsλŠ” Tagged-Template-Literals을 μ‚¬μš©ν•˜μ—¬ ꡬ성 μš”μ†Œμ˜ μŠ€νƒ€μΌμ„ μ§€μ •ν•œλ‹€.
  • Template Literal은 Javascriptμ—μ„œ μ œκ³΅ν•˜λŠ” 문법 쀑 ν•˜λ‚˜κ³ , Literal은 μ†ŒμŠ€ μ½”λ“œμ˜ κ³ μ •λœ 값을 μ˜λ―Έν•œλ‹€.
  • Template Literal은 λ‹€μŒκ³Ό 같이 백틱을 μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ μž‘μ„±ν•˜κ³  κ·Έ μ•ˆμ— λŒ€μ²΄ κ°€λŠ₯ν•œ Expression을 λ„£λŠ” 방법이닀.
1
2
3
4
5
6
7
import React from "react";
import styled from 'styled-components'

const Wrapper = styled.div`
    padding: 1em;
    background: grey;
`;

props μ‚¬μš©

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const Button = styled.button`
    color: ${props => props.dark ? "white" : "dark"};
    background: ${props => props.dark ? "black" : "white"};
    border: 1px solid black;
`;

function Sample(props) {
    return (
        <div>
            <Button>Normal</Button>
            <Button dark>Dark</Button>
        </div>
    )
}

export default Sample;
  • <Button dark>Dark</Button>μ—μ„œ props둜 darkλ₯Ό λ„£μ–΄ μ£Όκ³  μžˆλ‹€.
  • μ΄λ ‡κ²Œ λ“€μ–΄κ°„ propsλŠ” κ·ΈλŒ€λ‘œ Styled-Components둜 μ „λ‹¬λœλ‹€.

Styled-Components μŠ€νƒ€μΌ ν™•μž₯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const Button = styled.button`
    color: ${props => props.dark ? "white" : "dark"};
    background: ${props => props.dark ? "black" : "white"};
    border: 1px solid black;
`;

const RoundButton = styled(Button)`
    border-radius: 16px;
`;

function Sample(props) {
    return (
        <div>
            <Button>Normal</Button>
            <Button dark>Dark</Button>
            <RoundButton>Rounded</RoundButton>
        </div>
    )
}

export default Sample;
  • Styled-Componentsλ₯Ό μ‚¬μš©ν•˜λ©΄ React Componentκ°€ μƒμ„±λ˜λŠ”λ°, μ΄λ ‡κ²Œ μƒμ„±λœ Componentλ₯Ό 기반으둜 상기 μ½”λ“œμ™€ 같이 좔가적인 μŠ€νƒ€μΌμ„ μ μš©ν•  수 μžˆλ‹€.
1
2
3
const RoundButton = styled(Button)`
    border-radius: 16px;
`;
  • 상기 μ½”λ“œκ°€ μŠ€νƒ€μΌμ„ ν™•μž₯ν•˜λŠ” 핡심 μ½”λ“œλ‹€.

μ‹€μŠ΅

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Blocks.jsx
const Wrapper = styled.div`
    padding : 1rem;
    display : flex;
    flex-direction : row;
    align-items : flex-start;
    justify-content : flex-start;
    background-color : lightgrey;
`;

const Block = styled.div`
    padding : ${(props) => props.padding};
    border : 1px solid black;
    border-radius : 1rem;
    background-color : ${(props) => props.backgroundColor};
    color : white;
    font-size : 2rem;
    font-weight : bold;
    text-align : center;
`;

const blockItems = [
    {
        label: "1",
        padding: "1rem",
        backgroundColor: "red",
    },
    {
        label: "2",
        padding: "3rem",
        backgroundColor: "green",
    },
    {
        label: "3",
        padding: "2rem",
        backgroundColor: "blue",
    },
];

function Blocks(props) {
    return (
        <Wrapper>
            {blockItems.map((blockItem) => {
                return (
                    <Block
                        padding={blockItem.padding}
                        backgroundColor={blockItem.backgroundColor}
                    >
                        {blockItem.label}
                    </Block>
                );
            })}
        </Wrapper>
    );
}

export default Blocks;

회고

  • 일단 μ±… 읽은 뢀뢄을 λͺ¨λ‘ μ •λ¦¬ν•˜μ˜€μ§€λ§Œ, 아직 많이 λΆ€μ‘±ν•˜λ‹€.
  • 토이 ν”„λ‘œμ νŠΈλ₯Ό 톡해 배운 뢀뢄듀을 μ μš©ν•˜κ³  μ΄ν•΄ν•˜λŠ” 과정이 ν•„μš”ν•˜λ‹€.
This post is licensed under CC BY 4.0 by the author.