Code Convention
Naming
축약어를 사용하지 않고 작성한다.
→ ❌ btn ✅ button
Components Folder & Naming
┣ 📂components ┃ index.ts ┣ 📂HomePage ┃ ┣ 📂Button(component) ┃ ┃ ┣ Button.tsx ┃ ┃ ┣ Button.style.ts // Style 파일은 * 대신 named로 가져올 것 ┃ ┃ ┗ Button.stories.js
Components
// Button.tsx function Button() { return ( // ... ) } export default Button // index.ts export { default as Button } from './Button/Button'
Styling
// Button.style.ts import styled from '@emotion/styled' export const CustomButton = styled.button`` // Button.tsx import { CustomButton } from './Button.style' export function Button() { return ( <CustomButton >click me!</CustomButton> ) }
Functions
function Button() { const onClick = () => { //... } }
Import Order
- 리액트
- 라이브러리
- 모듈
- 스타일
// 리액트 import { useState, useEffect, ... } from 'react' // 라이브러리 import { throttle, debounce, ... } from "lodash"; // 모듈 // components, hooks, store, utils, assets, interface.. import { Button } from 'components' // 스타일 import { ButtonStyle } from './Button.style';
API Usage
// axiosInstance.ts import axios, { AxiosInstance } from 'axios'; const host = process.env.REACT_APP_API_HOST ?? 'localhost'; const port = process.env.REACT_APP_API_PORT ?? 3000; const API_ENDPOINT = `${host}:${port}`; const axiosInstance: AxiosInstance = axios.create({ baseURL: API_ENDPOINT, // baseURL 미리세팅 timeout: 5000, headers: { 'Content-Type': 'application/json', }, }); axiosInstance.interceptors.response.use( (response) => Promise.resolve(response), (error) => { console.error(error); return Promise.reject(error); }, ); export default axiosInstance;
// api/getPost.ts export const getPost = async (postId) => { const result = await axiosInstance({ method: 'GET', url: `/posts/${postId}` }) return result.data; };
Style
font, spacing:
rem
사용