Styled-components 공부(React)
                                            
                                                
                                                
                                                
                                                
                                                
                                                 7129 단어  styled-componentsReact공부 메모
                    
Styled-components를 사용하여 React에서 사용자 정의 버튼을 만들려고했습니다.
 CODESANDBOX 를 사용한 간단한 샘플은 이쪽.
 
 Custom Button 만들기
App.js
import CustomButton from "./components/CustomButton.component";
function App() {
  return (
    <div className="App">
      <h1>Let's Build Custom Buttons!</h1>
      <p>Basic Button Theme</p>
      <CustomButton onClick={() => {console.log('You have become a member!!')}}> Become a member </CustomButton>
      <p>Inverted Theme</p>
      <CustomButton inverted> Subscribe </CustomButton>
      <p>Large Size</p>
      <CustomButton large> Styled-components </CustomButton>
    </div>
  );
}
 절차
먼저 Custom Button 라는 구성 요소를 만듭니다.
내용은 아래와 같이・・・.
CustomButton.componentimport React from "react";
import { ButtonContainer } from "./CustomButton.styles";
const CustomButton = ({ children, ...otherProps }) => {
  return <ButtonContainer {...otherProps}> {children} </ButtonContainer>;
};
export default CustomButton;
ButtonContainer는 styled-components의 스타일링 이름입니다.
import CustomButton from "./components/CustomButton.component";
function App() {
  return (
    <div className="App">
      <h1>Let's Build Custom Buttons!</h1>
      <p>Basic Button Theme</p>
      <CustomButton onClick={() => {console.log('You have become a member!!')}}> Become a member </CustomButton>
      <p>Inverted Theme</p>
      <CustomButton inverted> Subscribe </CustomButton>
      <p>Large Size</p>
      <CustomButton large> Styled-components </CustomButton>
    </div>
  );
}
import React from "react";
import { ButtonContainer } from "./CustomButton.styles";
const CustomButton = ({ children, ...otherProps }) => {
  return <ButtonContainer {...otherProps}> {children} </ButtonContainer>;
};
export default CustomButton;
{children} 로 받습니다. ...otherProps 로 하여 여러 Props를 받을 수 있도록 합니다(ES6 문법)이 otherProps로 받은 속성은 어떻게 사용됩니까?
이것은 아래와 같습니다.
CustomButton.styles
import styled, { css } from "styled-components";
const invertedStyles = css`
  background-color: #065fd4;
  color: #fff;
`;
const largeStyles = css`
  width: 200px;
  height: 80px;
  font-size: 18px;
`;
const getStyles = props => {
  if (props.inverted) {
    return invertedStyles;
  }
  if (props.large) {
    return largeStyles;
  }
};
export const ButtonContainer = styled.button`
  width: 150px;
  height: 50px;
  background-color: #ffffff;
  color: #065fd4;
  border: 1px solid #065fd4;
  font-size: 14px;
  outline: none;
  cursor: pointer;
  ${getStyles}
`;
베이스가 되는
ButtonContainer 로 버튼의 스타일링을 실시해, ${getStyles} 로 Props의 내용에 의해 스타일링을 전환하도록 하고 있습니다.만약 props가 inverted라면 invertedStyles를 적용시키는 느낌이군요.
Reference
이 문제에 관하여(Styled-components 공부(React)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Abbiscuit/items/92ef06cbce074cde48af텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)