5. props와 attrs

Props

  • 단순 element인 경우 (ex. styled.div) 컴포넌트의 모든 HTML attribute가 DOM으로 전달됨

  • 커스텀 React Component인 경우 (ex. styled(MyComponent)) 스타일이 지정된 컴포넌트는 모든 프로퍼티를 전달함

// Create an Input component that'll render an <input> tag with some styles
const Input = styled.input`
  padding: 0.5em;
  margin: 0.5em;
  color: ${props => props.inputColor || "palevioletred"};
  background: papayawhip;
  border: none;
  border-radius: 3px;
`;

// Render a styled text input with the standard input color, and one with a custom input color
render(
  <div>
    <Input defaultValue="@probablyup" type="text" />
    <Input defaultValue="@geelen" type="text" inputColor="rebeccapurple" />
  </div>
);

typedefaultValue는 자동으로 DOM에 전달되지만, inputColor는 자동으로 전달되지 않음

타입을 지정해주자

attrs

컴포넌트의 기본 속성을 지정해줄 수 있음

Last updated