Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 타입
- 화살표함수
- 속성
- frontend
- ELSE
- boolean
- javascript
- 비교연산자
- flex
- 함수표현식
- 기초
- properties
- for of
- 프론트엔드
- 논리연산자
- 조건문
- 함수선언식
- 변수
- Methods
- if else
- 함수
- for in
- align-content
- justify-content
- typeof
- flex-wrap
- 문자열
- for
- flex-direction
- 반복문
Archives
- Today
- Total
하얀 코딩
[Styled Component - 6] ThemeProvider 본문
ThemeProvider는 styled-components 패키지에서 제공하는 컴포넌트 중 하나로,
테마(theme)를 애플리케이션 전역에서 사용할 수 있도록 해줍니다.
ThemeProvider를 사용하면 컴포넌트 계층 구조 어디에서나 일관된 테마를 사용할 수 있습니다.
ThemeProvider를 사용하려면 다음과 같이 styled-components와 함께 ThemeProvider 컴포넌트를 임포트하고
사용할 컴포넌트 계층 구조 내에 포함시키면 됩니다.
import React from 'react';
import { ThemeProvider } from 'styled-components';
const theme = {
colors: {
primary: '#fca311',
secondary: '#ffba08',
},
};
function App() {
return (
<ThemeProvider theme={theme}>
{/* 테마를 사용할 컴포넌트 계층 구조 */}
</ThemeProvider>
);
}
export default App;
위의 예제에서는 ThemeProvider 컴포넌트를 사용하여 theme 객체를 전달하고 있습니다.
theme 객체는 커스텀 테마를 정의하는데 사용됩니다.
theme 객체 안에는 원하는 테마 관련 속성들을 정의할 수 있습니다.
위의 예제에서는 colors라는 키를 가진 테마 속성을 정의했습니다.
import React from 'react';
import styled from 'styled-components';
const StyledButton = styled.button`
background-color: ${props => props.theme.colors.primary};
color: #ffffff;
padding: 8px 16px;
`;
function Button() {
return <StyledButton>Button</StyledButton>;
}
ThemeProvider 컴포넌트로 감싸진 하위 컴포넌트들은 theme 객체를 통해 테마에 접근할 수 있습니다.
styled-components에서는 theme 속성을 사용하여 테마 속성에 접근하거나 테마 속성을 동적으로 변경할 수 있습니다.
'Styled Components' 카테고리의 다른 글
[Styled Component - 8] createGlobalStyle (0) | 2023.08.30 |
---|---|
[Styled Component - 7] & (1) | 2023.06.05 |
[Styled Component - 5] keyframes (0) | 2023.06.05 |
[Styled Component - 3] attrs (0) | 2023.06.02 |
[Styled Component - 2] Props 활용하기 (0) | 2023.06.02 |