Atomic Habits

styled-components : styled.div` useState ` 본문

IT/Style(Library)

styled-components : styled.div` useState `

체계성 2021. 11. 10. 08:56
import React, { useState } from "react";
import styled from "styled-components";

function Example() {
  const [email, setEmail] = useState("");

  return (
    <ExampleWrap active={email.length}>
      <Button>Hello</Button>
      <NewButton color="blue">Im new Button</NewButton>
    </ExampleWrap>
  );
}

const ExampleWrap = styled.div`
  background: ${({ active }) => {
    if (active) {
      return "white";
    }
    return "#eee";
  }};
  color: black;
`;

const Button = styled.button`
  width: 200px;
  padding: 30px;
`;

// Button 컴포넌트 상속
const NewButton = styled.Button`
  // NewButton 컴포넌트에 color가는 props가 있으면 그 값 사용, 없으면 'red' 사용
  color: ${props => props.color || "red"};
`;

export default Example;

출처 : https://lienkooky.tistory.com/123

 

'IT > Style(Library)' 카테고리의 다른 글

React. Part2. ch2. 01-2. styled-components  (0) 2021.11.10
문제 : [object Object] - styled-components  (0) 2021.11.10
Comments