Heim > Web-Frontend > js-Tutorial > Hauptteil

React Basics~Styling-Komponente/css_ in _js

Barbara Streisand
Freigeben: 2024-10-09 22:47:28
Original
288 Leute haben es durchsucht
  • If we want to write the style as a css style in a javascript file, we can use the styled-components.

  • We need to install the styled components with a command like this npm i styled-components.

・src/Example.js

iimport { useState } from "react";
import styled from "styled-components";

const StyledButton = styled.button`
  margin: auto;
  border-radius: 9999px;
  border: none;
  display: block;
  width: 120px;
  height: 60px;
  font-weight: bold;
  cursor: pointer;
  background: ${({ isSelected }) => (isSelected ? "pink" : "")};

  @media (max-width: 600px) {
    border-radius: 0;
  }
`;

const OrangeButton = styled(StyledButton)`
  background: orange;

  :hover {
    color: red;
    opacity: 0.7;
  }

  span {
    font-size: 2em;
  }
`;

const Example = () => {
  const [isSelected, setIsSelected] = useState(false);

  const clickHandler = () => setIsSelected((prev) => !prev);

  return (
    <>
      <StyledButton isSelected={isSelected} onClick={clickHandler}>
        StyledButton
      </StyledButton>

      <OrangeButton isSelected={isSelected} onClick={clickHandler}>
        <span>OrangeButton</span>
      </OrangeButton>

      <div style={{ textAlign: "center" }}>{isSelected && "Clicked!"}</div>
    </>
  );
};

export default Example;

Nach dem Login kopieren
  1. We need to set the styled components as styeled.element.
    This is an example.
    const StyledButton = styled.button
    //styles
    ;.

  2. We can pass a prop to the styled components like this.
    from:
    to: background: ${({ isSelected }) => (isSelected ? "pink" : "")};

  3. We can set a media query in the styled components like this
    @media (max-width: 600px) {
    border-radius: 0;
    }

  4. We can implement the other style in the styled components like this.
    const OrangeButton = styled(StyledButton)

  5. We can set a pseudo-element in the styled components like this.
    :hover {
    color: red;
    opacity: 0.7;
    }

・The normal button(Gray button) before cliekd.
React Basics~styling component/ css_ in _ js

・The normal button(Pink button) after cliekd.
React Basics~styling component/ css_ in _ js

・The orange button before cliekd.
React Basics~styling component/ css_ in _ js

・The orange button after cliekd.
React Basics~styling component/ css_ in _ js

Das obige ist der detaillierte Inhalt vonReact Basics~Styling-Komponente/css_ in _js. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!