๐Ÿ‘ฉโ€๐Ÿ’ป
Megaptera Frontend
  • ์ฃผ์ฐจ๋ณ„ ํ•™์Šต
    • megaptera-front
    • 1. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ
      • 1. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ
      • 2. TypeScript
      • 3. React
      • 4. Testing Library
      • 5. Parcel & ESLint
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 2. JSX
      • 1. JSX
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 3. React๋กœ ์‚ฌ๊ณ ํ•˜๊ธฐ
      • 1. React Component
      • 2. React State
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 4. React Hooks
      • 1. Express
      • 2. Fetch API & CORS
      • 3. React์˜ Hook
      • 4. useRef & Custom Hook
      • 5. usehooks-ts
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 5. ํ…Œ์ŠคํŠธ
      • 1. TDD
      • 2. React Testing Library
      • 3. MSW
      • 4. Playwright
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 6. External Store
      • 1. External Store
      • 2. TSyringe
      • 3. Redux ๋”ฐ๋ผํ•˜๊ธฐ
      • 4. usestore-ts
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 7. React Router
      • 1. Routing
      • 2. Routes
      • 3. Router
      • 4. Navigation
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 8. CSS in JS
      • 1. Design System
      • 2. Style Basics
      • 3. CSS in JS
      • 4. styled-components
      • 5. props์™€ attrs
      • 6. Global Style & Theme
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 9. ์‡ผํ•‘๋ชฐ ๋ชฉ๋ก, ์ƒํ’ˆ ํŽ˜์ด์ง€
      • 1. ๊ฐœ๋ฐœํ•˜๊ธฐ ์ „ ์ค€๋น„
      • 2. ๋ชฉ๋ก ๋ณด๊ธฐ
      • 3. ์ƒํ’ˆ ์ƒ์„ธ ๋ณด๊ธฐ
      • 4. ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ณด๊ธฐ
      • 5. ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ƒํ’ˆ ๋‹ด๊ธฐ
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 10. ์‚ฌ์šฉ์ž ์ธ์ฆ, ์ธ๊ฐ€
      • 1. ๋กœ๊ทธ์ธ
      • 2. ๋กœ๊ทธ์•„์›ƒ
      • 3. ํšŒ์›๊ฐ€์ž…
      • 4. ์ฃผ๋ฌธ ๋ชฉ๋ก & ์ฃผ๋ฌธ ์ƒ์„ธ
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 11. ์ฃผ๋ฌธ, ๊ฒฐ์ œ
      • 1. ๋ฐฐ์†ก ์ •๋ณด ์ž…๋ ฅ
      • 2. ํฌํŠธ์› ๊ฒฐ์ œ ์š”์ฒญ
      • 3. ๋ฐฐ์†ก ๋ฐ ๊ฒฐ์ œ ์ •๋ณด ์ „๋‹ฌ
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
    • 12. ์–ด๋“œ๋ฏผ
      • 1. ๊ด€๋ฆฌ์ž ์›น ์‚ฌ์ดํŠธ ๊ฐœ๋ฐœ ์‹œ์ž‘
      • 2. ๋กœ๊ทธ์ธ, ์‚ฌ์šฉ์ž ๋ชฉ๋ก
      • 3. ์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ฆฌ
      • 4. ์ฃผ๋ฌธ ๊ด€๋ฆฌ
      • 5. ์ƒํ’ˆ ๊ด€๋ฆฌ
      • ํ•œ ์ฃผ๋ฅผ ๋งˆ์น˜๋ฉฐ
Powered by GitBook
On this page
  • 1. Props
  • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
  • 2. attrs
  • .attrs
  • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
  1. ์ฃผ์ฐจ๋ณ„ ํ•™์Šต
  2. 8. CSS in JS

5. props์™€ attrs

Previous4. styled-componentsNext6. Global Style & Theme

Last updated 2 years ago

1. Props

์†์„ฑ์„ ๋ฐ›์•„์„œ ์“ฐ๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉ

๋Œ€์ƒ์ด ๋‹จ์ˆœํ•œ ์š”์†Œ์ผ ๊ฒฝ์šฐ, styled-components๋Š” HTML ์†์„ฑ์„ ํ†ตํ•ด DOM์œผ๋กœ ์ „๋‹ฌ ์‚ฌ์šฉ์ž ์ง€์ • React ์ปดํฌ๋„ŒํŠธ์ธ ๊ฒฝ์šฐ, ์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ชจ๋“  props๋ฅผ ํ†ต๊ณผํ•จ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์„ ์ด์šฉํ•ด์„œ ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ ๋“ฑ์„ ํ‘œํ˜„

  • ํ™œ์„ฑํ™” ์—ฌ๋ถ€ ํ‘œํ˜„

  • ํŠน์ • ์Šคํƒ€์ผ์„ ์žก์•„์ฃผ๊ณ  ์‹ถ์„ ๋•Œ

  • ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ๋ง ๊ฐ€๋Šฅ

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

import { useBoolean } from 'usehooks-ts';

import styled, { css } from 'styled-components';

type ParagraphProps = { 
    active?: boolean;
}

const Paragraph = styled.p<ParagraphProps>`
    color: ${(props) => (props.active ? '#F00' : '#888')};
    ${(props) => props.active && css`
    	font-weight: bold;
    `}
`;

export default function Greeting() {
    const { value: active, toggle } = useBoolean(false);
    
    return (
        <div>
            <Paragraph>Inactive</Paragraph>
            <Paragraph active>Active</Paragraph>
            <Paragraph active={active}>
                Hello, world
                {' '}
                <button type="button" onClick={toggle}>
                    Toggle
                </button>
            </Paragraph>
        </div>
    );
}

๐Ÿ’ก styled-components ์˜ { css } ๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ๋‹จ์ˆœ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ CSS ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ ๐Ÿ’ก <ParagraphProps> ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด <{ active: boolean }> ์˜ ํ˜•ํƒœ๋กœ ์ง์ ‘ ์ ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ

โœ๏ธ ์กฐ๊ฐ๊ธ€

์™œ props๋ฅผ ์ด์šฉํ•  ๋•Œ ${(props) => props.active && css} ์˜ ๋ฐฉ์‹์œผ๋กœ ์ ๋Š”์ง€ ๋ชฐ๋ž๋Š”๋ฐ css๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ž‘์„ฑํ•ด๋ณด๋‹ˆ ๋ถˆํŽธํ•จ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์Šคํƒ€์ผ ์ฝ”๋“œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ธ์‹ํ•ด์„œ CSS๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค. ์ž˜ ๋ชจ๋ฅด๋ฉด์„œ ์›๋ž˜ ๊ทธ๋Ÿฐ๊ฐ€๋ณด๋‹ค ํ•˜๊ณ  ์ผ๋˜ styled๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด์„œ๋„ ์•Œ๊ฒŒ ๋๊ณ , css์˜ ์กด์žฌ ์ด์œ ๋„ ์•Œ๊ฒŒ ๋˜์–ด ์ฆ๊ฒ๋‹ค.

2. attrs

๊ธฐ๋ณธ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ์†์„ฑ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ ์šฉ

  • <button> ๋“ฑ์„ ๋งŒ๋“ค ๋•Œ ์ ๊ทน ํ™œ์šฉ

    • type="button" ๋“ฑ์„ ๋ฌด์กฐ๊ฑด ์จ์•ผ ํ•˜๋Š”๋ฐ, ์•ˆ ์“ฐ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ

    • styled.button.attrs({ type: 'button' }) ํ˜•ํƒœ๋กœ ์ž‘์„ฑ

.attrs

์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ์— ์ผ๋ถ€ props๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์—ฐ๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฉ”์†Œ๋“œ ์ฒซ ๋ฒˆ์งธ์ด์ž ์œ ์ผํ•œ ์ธ์ˆ˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ๋‚˜๋จธ์ง€ props์— ๋ณ‘ํ•ฉ๋  ๊ฐ์ฒด

๊ฐ ๋ž˜ํผ๊ฐ€ .attrs์˜ ์ค‘์ฒฉ ์‚ฌ์šฉ์„ ์žฌ์ •์˜(override) ํ•  ์ˆ˜ ์žˆ์Œ ์Šคํƒ€์ผ์‹œํŠธ์—์„œ ๋‚˜์ค‘์— ์ •์˜๋œ css ์†์„ฑ์ด ์ด์ „ ์„ ์–ธ์„ ๋ฎ์–ด์“ฐ๋Š” ๋ฐฉ์‹๊ณผ ์œ ์‚ฌ

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

import styled from 'styled-components';

const Button = styled.button.attrs({
    type: 'button',
})`
    border: 1px solid #888;
    background: transparent;
    cursor: pointer;
`;

export default Button;
// button์˜ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ 
type ButtonProps = { 
    type?: 'button' | 'submit' | 'reset'; 
    active?: boolean;
};

// attrs์˜ <ButtonProps>
const Button = styled.button.attrs<ButtonProps>(props => ({
  type: props.type ?? 'button',
}), 
// style์„ ์œ„ํ•œ <ButtonProps> 
)<ButtonProps>` 
    background: #FFF;
    color: #000;
    border: 1px solid ${(props: ButtonProps) => (props.active ? '#F00' : '#888')};
	
	${props => props.active && css`
	    background: #00F;
	    color: #FFF;
	`}
`;

export default function Switch() {
  const {value: active, toggle} = useBoolean(false);

  return (
      // button ํƒ€์ž…์„ ์ง€์ •ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ์—์„œ attrs ์„ค์ •๋Œ€๋กœ ์ง€์ •๋จ
      <Button onClick={toggle} active={active}>
        On/Off
      </Button>
  );
}

Passed props
๐Ÿ”— ์‹ค์Šต ๋งํฌ
Attaching additional props
.attrs
๐Ÿ”— ์‹ค์Šต ๋งํฌ