π 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.