ram2 ๐Ÿš—

[react] ThemeProvider ์‚ฌ์šฉํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ–ฅ๏ธ react

[react] ThemeProvider ์‚ฌ์šฉํ•˜๊ธฐ

coram22 2024. 1. 20. 23:48
728x90
๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ๏ธ  ThemeProvider ?

์ง€์ •๋œ ๋””์ž์ธ ์‹œ์Šคํ…œ์„ ๋ชจ๋“  ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ !?

๊ทธ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ThemeProvider์ด๋‹ค.

React ์ปดํฌ๋„ŒํŠธ์—์„œ ํ…Œ๋งˆ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ณตํ†ต theme์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ‰์ƒ๊ฐ’์€ props๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•œ๋‹ค.

 

 

๐Ÿ–ฅ๏ธ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ• ๊นŒ?

1. theme ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ , ํ•ด๋‹น ํŒŒ์ผ์— ์ƒ‰์ƒ์„ ์ง€์ •ํ•œ๋‹ค.

๋‹ค์Œ์€ ๋‚ด๊ฐ€ ์ง€์ •ํ•œ theme ์ƒ‰์ƒ์ด๋‹ค. 

// styles/theme.js

const theme = {
  colors: {
    background: "#121212",
    text: "#ececec",
    primary: "#63e6be",
    primary2: "#62E6BE",
    white1: "#F1F3F5",
    white: "#FFFFFF",
    unSelected: "#ACACAC",
    secondBlack: "#1E1E1E",
    divider: "#2A2A2A"
  },
};

export default theme;

 

 

2. import ํ•˜๊ธฐ

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ํŒŒ์ผ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒ๋‹จ์— import ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

import { ThemeProvider } from "styled-components";
import theme from "../../styles/theme";

 

 

3. App.js์— ์‚ฌ์šฉํ•˜๊ธฐ

import theme from "./styles/theme";
import Router from "./router/Router";

function App() {
  return (
    <ThemeProvider theme={theme}>
      <GlobalStyle />
      <Router />
    </ThemeProvider>
  );
}

export default App;

์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ThemeProvider์˜ props๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค !!

 

 

4. ์‹ค์ œ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ

background-color: ${theme.colors.background};

 

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, theme ํŒŒ์ผ์— ์žˆ๋Š” ์ƒ‰์ƒ๋“ค์„ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค !!

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•