Skip to content

โœ๐Ÿป code convention

Jiyeon Baek edited this page Oct 9, 2022 · 2 revisions

1. styled-components

  • ์ž๋™ ์ •๋ ฌ
    (์ฐธ๊ณ ) ํ˜„์—…์—์„œ ์‚ฌ์šฉ์ค‘์ธ CSS ์ž‘์„ฑ ์ˆœ์„œ์™€ ์‰ฝ๊ฒŒ ์ž๋™ ์ •๋ ฌํ•˜๊ธฐ(VScode ํ™•์žฅํ”„๋กœ๊ทธ๋žจ)

  • ๋ชจ์งˆ๋ผ์—์„œ ์ œ์•ˆํ•œ CSS ์†์„ฑ ๊ธฐ์ˆ  ์ˆœ์„œ ๋”ฐ๋ฅด๊ธฐ
    (์ฐธ๊ณ ) CSS ์„ ์–ธ์ˆœ์„œ

  • ์ฝ”๋“œ ํ•˜๋‹จ์— ์ž‘์„ฑ

  • ์Šคํƒ€์ผ.ts ๋”ฐ๋กœ ๋นผ์ง€ ์•Š๊ณ  ์ปดํฌ๋„ŒํŠธ.tsx ํ•œ ํŒŒ์ผ์— ๊ฐ™์ด ์ž‘์„ฑ

  • px ๋‹จ์œ„ ๋Œ€์‹  rem ๋‹จ์œ„ ์‚ฌ์šฉ (10px === 1rem)

  • ํ•ญ์ƒ ๋งจ ์•ž์—ย Stย ๋ถ™์—ฌ์ฃผ๊ธฐ

  • ์ตœ์ƒ๋‹จ ํƒœ๊ทธ ์ด๋ฆ„์€ ย St[์ปดํฌ๋„ŒํŠธ๋ช…]

    function Home() {
      return (
        <StHome>
        ...
        </StHome>
      );
    }
  • container์™€ wrapper ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ

    container๋Š” ์ฃผ๋กœย ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ๊ฐ์‹ธ๋Š” div์ด๊ณ , wrapper๋Š”ย ๋‹จ์ผ ์š”์†Œ์˜ ๋ ˆ์ด์•„์›ƒ์„ ์œ„ํ•œ div๋ฅผ ๋งํ•œ๋‹ค.

  • (์ž์œจ) ๊ฐ€๋…์„ฑ๊ณผ ์ถ”ํ›„ ํ™•์žฅ์„ ๊ณ ๋ คํ•˜์—ฌ css ์†์„ฑ ๊ฐ„ ๊ฐœํ–‰์„ ์ƒํ™œํ™”

  • (์ž์œจ) ๋„ค์ด๋ฐ ์ฐธ๊ณ 

    • div : '์ปดํฌ๋„ŒํŠธ๋ช…'Box
    • section : '์ปดํฌ๋„ŒํŠธ๋ช…'Section
    • ul : '์ปดํฌ๋„ŒํŠธ๋ช…'List
    • li : '์ปดํฌ๋„ŒํŠธ๋ช…'Item
    • p : '์ปดํฌ๋„ŒํŠธ๋ช…'Paragraph
    • span : '์ปดํฌ๋„ŒํŠธ๋ช…'Span

2. ํด๋”๋ช…

  • components ํด๋” ์•„๋ž˜์—์„œ ์†Œ๋ฌธ์ž ํด๋”๋ช… ์‚ฌ์šฉ
    • page๋ช…(home, learn, review)
    • ๋‘ ๋‹จ์–ด ์ด์ƒ์ด๋ฉด camel case ์‚ฌ์šฉ

3. Event handler

  • Function Names

    on~~ = {handle~~}

    <MyComponent onClick={handleClick} />
  • ์ข€ ๋” ๋ณต์žกํ•œ ๋„ค์ด๋ฐ

    onํŠน์ง•~~ = {handleํŠน์ง•~~}

    <MyComponent
      onAlertClick={handleAlertClick}
      onFormSubmit={handleFormSubmit}
    />
  • ์ƒํ™ฉ์— ๋งž๊ฒŒ 2๊ฐ€์ง€ ๋ฐฉ์‹ ํ˜ผ์šฉ

4. ๋ณ€์ˆ˜

  • camel case ์‚ฌ์šฉ (ex. camelCase)
  • ๋ณ€์ˆ˜๋Š” var ์‚ฌ์šฉ ๊ธˆ์ง€, ๊ทธ๋ƒฅ const !
  • ์˜ˆ์™ธ : ์ƒ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด ๋Œ€๋ฌธ์ž, ์–ธ๋”๋ฐ” ์กฐํ•ฉ โ†’ EXAMPLE_NAME
  • ๋ฐฐ์—ด๊ณผ ๊ด€๋ จ๋œ ๋ณ€์ˆ˜๋ช…์€ __List (s ์‚ฌ์šฉ ๊ธˆ์ง€)
  • ๋ณ€์ˆ˜ ๋„ค์ด๋ฐ ์•„๋ž˜ ์ฐธ๊ณ 
    buttonSearch
    searchButton โœ…
    btnSearch
    searchBtn

5. ํ•จ์ˆ˜

  • camel case ์‚ฌ์šฉ (ex. camelCase)
  • ํ•จ์ˆ˜๋ช…์€ ๋™์‚ฌ๋กœ ์‹œ์ž‘
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ
    const ํ•จ์ˆ˜๋ช… = () => {}

6. ์ปดํฌ๋„ŒํŠธ

  • pascal case ์‚ฌ์šฉ (ex. PascalCase)
  • ์•„๋ž˜ ํ˜•์‹๋Œ€๋กœ ์‚ฌ์šฉ
    interface ์ปดํฌ๋„ŒํŠธ๋ช…Props
      
    function ์ปดํฌ๋„ŒํŠธ๋ช…(props: ์ปดํฌ๋„ŒํŠธ๋ช…Props) {
      const { ๋ณ€์ˆ˜1, ๋ณ€์ˆ˜2, ๋ณ€์ˆ˜3 } = props;
        return (
      )
    }
      
    export default ์ปดํฌ๋„ŒํŠธ๋ช…;
      
    // styled-components

7. ์ด๋ฏธ์ง€

ํ”ผ๊ทธ๋งˆ์—์„œ svg๋ฅผ exportํ•ด์„œย src/assets/icons ํ˜น์€ย src/assets/images์— ์ €์žฅํ•˜๊ณ ,ย src/assets/icons/index.ts์™€ src/assets/images/index.ts์— ์•„๋ž˜์ฒ˜๋Ÿผ ์ถ”๊ฐ€ํ•ด ์‚ฌ์šฉ

// ์•„์ด์ฝ˜ ์˜ˆ์‹œ
export { default as icSample } from './icons/ic_sample.svg';
export { ReactComponent as IcSetting } from './ic_setting.svg';

// ์ด๋ฏธ์ง€ ์˜ˆ์‹œ
export { default as imgSample } from './images/img_sample.svg';
export { ReactComponent as ImgNewLink } from './img_new_link.svg';

8. ์ฃผ์„

๋ฆด๋ฆฌ์ฆˆ ์‹œ, ์ฃผ์„ ์ œ๊ฑฐ ์˜ˆ์ • ์ฝ”๋“œ ๋ฐ”๋กœ ์œ„์— ์ฃผ์„ ๋‹ฌ๊ธฐ

// ํ•œ ์ค„์งœ๋ฆฌ
/* ์—ฌ๋Ÿฌ ์ค„ */