default props
App.tsx
import Circle from "./Circle";
function App() {
return (
<div>
<Circle bgColor={'teal'}/>
<Circle bgColor={'tomato'}/>
</div>
);
}
export default App;
Circle.tsx
import React from "react";
import styled from "styled-components"
interface ContainerProps {
bgColor : string;
}
const Container = styled.div<ContainerProps>`
width : 200px;
height : 200px;
background-color : ${(props) => props.bgColor};
`;
interface CircleProps {
// TypeScript μκ² bgColor λ λ¬Έμμ΄μ΄λΌκ³ μλ¦Ό
bgColor : string;
}
const Circle = ({bgColor} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor}/>
</React.Fragment>
)
}
export default Circle;
λ§μ½ App.js μμ
import Circle from "./Circle";
function App() {
return (
<div>
<Circle />
<Circle bgColor={'tomato'}/>
</div>
);
}
export default App;
- μ΄λ κ²λ§ λμ΄λ, μλ¬κ° λ°μ ν©λλ€.
- μ΄μ λ νμ¬ Circle μ»΄ν¬λνΈμμ bgColor λ νμ μ λλ€.
- λ§μ½, νμκ° μλ μ νμ μΌλ‘ λ§λ€κ³ μΆλ€λ©΄ ??
optional props
ex) λ κ°μ μμ΄ μλλ° κ·Έ μ€ νλμ μμλ§ border-color λ₯Ό κ°μ§κ² νκ³ μΆλ€.
Circle.tsx
interface CircleProps {
bgColor : string;
borderColor : string;
}
const Circle = ({bgColor} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor}/>
</React.Fragment>
)
}
- App.js μμ μλ¬κ° λ°μν©λλ€.
- μ΄μ λ, App.js μμ borderColor λ₯Ό 보λ΄λ κ²μ μνκΈ° λλ¬Έμ λλ€.
- κ·Έλμ ν κ²μ νμκ° μλ μ νμ μΌλ‘ λ§λ€μ΄ μ£Όλ κ² μ λλ€.
? (optional)
interface CircleProps {
bgColor : string;
borderColor? : string;
}
- ? λ₯Ό λΆμ΄λ©΄ μ νμ μ΄κ² λ³ν©λλ€. (App.js μμ μλ¬κ° μ¬λΌμ§λλ€.)
- borderColor λ string λλ undefined λΌκ³ ν©λλ€.
νμ§λ§
const Circle = ({bgColor, borderColor} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor} borderColor={borderColor}/>
</React.Fragment>
)
}
- μμ κ°μ κ²½μ°μλ Container λ borderColor λ₯Ό λͺ°λΌμ μλ¬λ₯Ό λνλ λλ€.
- λ§μ°¬κ°μ§λ‘ Container μ interface μ μλ €μ€μΌ ν©λλ€.
Container - interface (style-component)
interface ContainerProps {
bgColor : string;
borderColor : string;
}
- νμ§λ§ μΆκ°λ§ νλ€κ³ μλ¬κ° μ¬λΌμ§μ§ μμ΅λλ€.
- μ΄μ λ, Circle μμμλ borderColor κ° νμκ° μλλΌ μ νμ μ΄κΈ° λλ¬Έμ λλ€.
μ΄λ₯Ό μ€μ¬νκΈ° μν΄μ
App.tsx
import Circle from "./Circle";
function App() {
return (
<div>
<Circle bgColor={'teal'} borderColor={'yellow'}/>
<Circle bgColor={'tomato'}/>
</div>
);
}
export default App;
- 첫 μμλ§ borderColor λ₯Ό 보λ΄λ μλ¬κ° λ¨μ§ μλ μ΄μ λ,
- νμ μμμΈ bgColor λ λ λ€ λ³΄λ΄μ£Όκ³ μκ³
- borderColor λ string λλ undefined κ° λ μ μκ² μ νμ μΌλ‘ λ°λ μ μμΌλκΉ
Circle.tsx
const Circle = ({bgColor, borderColor} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor} borderColor={borderColor ?? 'white'}/>
</React.Fragment>
)
}
- νμͺ½ μμμλ borderColor κ° 'yellow' κ° λ κ²μ΄λΌλ κ±Έ μκ³ μμ΅λλ€.
- νμ§λ§ νμͺ½μμλ undefined κ° λ μ μλ€λ κ±Έ μκ³ μμ΅λλ€.
- μ΄λ₯Ό ν΄κ²°νκΈ° μν΄ TypeScript μκ² μ΄κΈ°κ°μ μ€ μ μμ΅λλ€.
??
λ₯Ό μ¬μ©ν΄μ 'borderColor λ μ¬μ©μκ° λ§λ κ°μ μ¬μ©νλ©°, λ§μ½ undefined μνλΌλ©΄ μ΄κΈ°κ°μ μ¬μ©ν΄λΌ` λΌκ³ μ λ¬ ν©λλ€- μ΄λ κ² λλ©΄, λ μ΄μ μλ¬κ° λ°μνμ§ μμ΅λλ€.
μ 체 μ½λ
App.tsx
import Circle from "./Circle";
function App() {
return (
<div>
<Circle bgColor={'teal'} borderColor={'yellow'}/>
<Circle bgColor={'tomato'}/>
</div>
);
}
export default App;
Circle.tsx
import React from "react";
import styled from "styled-components"
interface ContainerProps {
bgColor : string;
borderColor : string;
}
const Container = styled.div<ContainerProps>`
width : 200px;
height : 200px;
border-radius : 50%;
background-color : ${(props) => props.bgColor};
border : 5px solid ${(props) => props.borderColor};
`;
interface CircleProps {
bgColor : string;
borderColor? : string;
}
const Circle = ({bgColor, borderColor} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor} borderColor={borderColor ?? 'white'}/>
</React.Fragment>
)
}
export default Circle;
μ°μ΅
interface CircleProps {
bgColor : string;
borderColor? : string;
text? : string;
}
const Circle = ({bgColor, borderColor, text="default Text"} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor} borderColor={borderColor ?? 'white'}>
{text}
</Container>
</React.Fragment>
)
}
- λ§μ½ props λ‘ text λ₯Ό λ°μ§ μμ§λ§, νλ©΄μ μΆλ ₯νκ³ μμ λλ
- μμ λ°©μ μ²λΌ μ΄κΈ°κ°μ μ€ μ μμ΅λλ€.
μ°Έκ³
- λ Έλ§λμ½λ
'π Front-End > React.js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
react-router-dom V6 κ³΅λΆ (0) | 2022.11.11 |
---|---|
React + TypeScript(state) (0) | 2022.10.26 |
React + TypeScript(interface) (0) | 2022.10.26 |
useInfiniteQuery νΈμΆ (0) | 2022.10.26 |
React Query - 무ν μ€ν¬λ‘€(Infinite Scroll) (0) | 2022.10.26 |