[TIL] ReactJs ๐Ÿ’…styled-components attributes ๋‹ค๋ฃจ๊ธฐ

G E Leeยท2022๋…„ 10์›” 19์ผ
0
post-thumbnail

์ปดํฌ๋„ŒํŠธ์— attribute ๊ณ ์ •์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ

// input ์— required ์ ์šฉ
import styled from "styled-components";

function App() {
  return (
    <div>
      <form>
        <Input />
        <Input />
        <Input />
        <button type="submit">submit</button>
      </form>
    </div>
  );
}

const Input = styled.input.attrs({ required: true })`
  color: skyblue;
`;
export default App;

styled-components input required

attribute๋ฅผ props๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๊ธฐ

// box ์ปดํฌ๋„ŒํŠธ์— ๊ธ€์ž ์ƒ‰์ƒ๊ณผ ๋ฐ•์Šค bg ์ƒ‰์ƒ ๊ฐ๊ฐ ์ ์šฉํ•˜๊ธฐ
import styled from "styled-components";

function App() {
  return (
    <div>
      <Box fontColor="white" bgColor="#222">
        Box1
      </Box>
      <Box fontColor="yellow" bgColor="tomato">
        Box2
      </Box>
      <Box fontColor="blue" bgColor="#dbdbdb">
        Box3
      </Box>
    </div>
  );
}

const Box = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100px;
  height: 100px;
  background-color: ${(props) => props.bgColor};
  color: ${(props) => props.fontColor};
`;
export default App;

styled-components box attr props

์ปดํฌ๋„ŒํŠธ์— selector ์‚ฌ์šฉํ•˜๊ธฐ

import styled from "styled-components";

function App() {
  return (
    <div>
      <Input placeholder="hello!" />
      <Input placeholder="world!" />
    </div>
  );
}
const Input = styled.input`
  background-color: beige;
  &:focus {
    color: white;
    background-color: #222;
  }
`;

export default App;

styled-components input selector

profile
๋ฐฐ์›€์€ ๋์ด ์—†๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€