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 | 29 | 30 | 31 |
Tags
- nextjs 설치
- icon
- createGlobalStyle
- API 토큰
- API token
- SCSS
- getModifierState
- bootstrap
- rewrites
- typescript react
- git lab
- github io
- CSS
- dart 변수
- nextjs
- github
- fetch
- nextjs .env
- react env
- next.js css
- 컨디셔널 렌더링
- styled components
- react typescript
- npm styled-reset
- Git
- react
- ngrok실행
- input type=file
- ngrok설치
- There isn’t anything to compare
Archives
- Today
- Total
꾸준히 성장하는 개발자
[React]styled-component 를 이용한 theme 본문


dark mode light mode
styled components를 이용하면 테마를 이렇게 바꾸는 것이 가능하다
//Index.js
import React from "react";
import ReactDOM from "react-dom";
import { ThemeProvider } from "styled-components";
import App from "./App";
const darkTheme = {
textColor: "whitesmoke",
backgroundColor: "#111",
};
const lightTheme = {
textColor: "#111",
backgroundColor: "whitesmoke",
};
ReactDOM.render(
<React.StrictMode>
<ThemeProvider theme={lightTheme}>
<App />
</ThemeProvider>
</React.StrictMode>,
document.getElementById("root")
);
import { ThemeProvider } from "styled-components"
import { ThemeProvider } from "styled-components";
-> 먼저 { ThemeProvider }를 import해준다
그리고 <ThemeProvider> </ThemeProvider>로 적용할 부분은 감싸주고
하나의 prop이 필요한데 그게 theme라는 prop이다
<ThemeProvider theme={lightTheme}> </ThemeProvider>
그리고 위 theme의 이름이 바뀌면서 모드의 색상들은 바뀌게 된다.
그리고 아래처럼 어떤 모드일 때 어떤 색상 디자인을 할지 object를 만들어준다
const darkTheme = {
textColor: "whitesmoke",
backgroundColor: "#111",
};
const lightTheme = {
textColor: "#111",
backgroundColor: "whitesmoke",
};
주의할 점!!! property의 이름이 같을 것!!!!
//App.js
import styled, { keyframes } from "styled-components";
;
const Title = styled.h1`
color: ${(props) => props.theme.textColor};
`;
const Wrapper = styled.div`
display: flex;
height: 100vh;
width: 100vw;
justify-content: center;
align-items: center;
background-color: ${(props) => props.theme.backgroundColor};
`;
function App() {
return (
<Wrapper>
<Title>hello</Title>
</Wrapper>
);
}
export default App;
그리고 color, backgroundColor 등 위처럼 작성하면 theme에 따라 색상은 바뀌게 된다.
color: ${(props) => props.theme.textColor};
background-color: ${(props) => props.theme.backgroundColor};
<ThemeProvider theme={lightTheme}> </ThemeProvider>
그리고 위 theme의 이름이 바뀌면서 다크 모드, 라이트 모드 등으로 바꿀 수 있다
'React' 카테고리의 다른 글
[에러] Need to install the following packages: create-react-app (0) | 2022.04.29 |
---|---|
[React]useState( ) (0) | 2022.04.27 |
[React] props type (0) | 2022.02.28 |
[react] Ant Design (0) | 2022.02.10 |
[react] react-shadow (0) | 2022.02.09 |