[React] 초기 세팅은 이렇게

sjoleee·2022년 7월 19일
0
post-custom-banner

CRA로 typescript 개발환경 설정하기

npx create-react-app "my-app" --template typescript

styled-components

npm i --save-dev @types/styled-components

Create a declarations file

//styled.d.ts
import "styled-components";

declare module "styled-components" {
  export interface DefaultTheme {
    bgColor: string;
    textColor: string;
    accentColor: string;
    cardColor: string;
  }
}

ThemeProvider

ThemeProvider를 통해 App 내에서 theme에 자유롭게 접근 가능.

//index.tsx
  <React.StrictMode>
      <ThemeProvider theme={Theme}>
        <App />
      </ThemeProvider>
  </React.StrictMode>
//theme.ts
import { DefaultTheme } from "styled-components";

export const Theme: DefaultTheme = {
  bgColor: "black",
  textColor: "whitesmoke",
};

createGlobalStyle

createGlobalStyle을 통해 globalStyle 적용

//App.tsx
import React from "react";
import styled, { createGlobalStyle } from "styled-components";

const GlobalStyle = createGlobalStyle`
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, menu, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
main, menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, main, menu, nav, section {
  display: block;
}
/* HTML5 hidden-attribute fix for newer browsers */
*[hidden] {
    display: none;
}
body {
  line-height: 1;
}
menu, ol, ul {
  list-style: none;
}
blockquote, q {
  quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
`;

function App() {
  return (
    <>
      <GlobalStyle />
    </>
  );
}

export default App;

Recoil

RecoilRoot

//index.tsx
  <React.StrictMode>
    <RecoilRoot>
      <ThemeProvider theme={Theme}>
        <App />
      </ThemeProvider>
    </RecoilRoot>
  </React.StrictMode>
profile
상조의 개발일지
post-custom-banner

0개의 댓글