5. props-attrs

style ์—ฐ์Šต

ํ™œ์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ํ‘œํ˜„ํ•˜๊ฑฐ๋‚˜, ํŠน์ • ์Šคํƒ€์ผ์„ ์žก์•„์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•จ.

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>
  );
}

์†์„ฑ ์ถ”๊ฐ€

๊ธฐ๋ณธ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ. ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ์†์„ฑ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ ์šฉํ•œ๋ฐ, ๋ฒ„ํŠผ ๋“ฑ์„ ๋งŒ๋“ค ๋•Œ ์ ๊ทน ํ™œ์šฉํ•จ.

import styled from "styled-components";

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

export default Button;

Last updated