React Basics~styling component/ css_ in _ js
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;
We need to set the styled components as styeled.element.
This is an example.
const StyledButton = styled.button
//styles
;.We can pass a prop to the styled components like this.
from:
to: background: ${({ isSelected }) => (isSelected ? "pink" : "")};We can set a media query in the styled components like this
@media (max-width: 600px) {
border-radius: 0;
}We can implement the other style in the styled components like this.
const OrangeButton = styled(StyledButton)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.
・The normal button(Pink button) after cliekd.
・The orange button before cliekd.
・The orange button after cliekd.
The above is the detailed content of React Basics~styling component/ css_ in _ js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

TypeScript's advanced condition types implement logical judgment between types through TextendsU?X:Y syntax. Its core capabilities are reflected in the distributed condition types, infer type inference and the construction of complex type tools. 1. The conditional type is distributed in the bare type parameters and can automatically split the joint type, such as ToArray to obtain string[]|number[]. 2. Use distribution to build filtering and extraction tools: Exclude excludes types through TextendsU?never:T, Extract extracts commonalities through TextendsU?T:Never, and NonNullable filters null/undefined. 3

Microfrontendssolvescalingchallengesinlargeteamsbyenablingindependentdevelopmentanddeployment.1)Chooseanintegrationstrategy:useModuleFederationinWebpack5forruntimeloadingandtrueindependence,build-timeintegrationforsimplesetups,oriframes/webcomponents

varisfunction-scoped,canbereassigned,hoistedwithundefined,andattachedtotheglobalwindowobject;2.letandconstareblock-scoped,withletallowingreassignmentandconstnotallowingit,thoughconstobjectscanhavemutableproperties;3.letandconstarehoistedbutnotinitial

Optionalchaining(?.)inJavaScriptsafelyaccessesnestedpropertiesbyreturningundefinedifanypartofthechainisnullorundefined,preventingruntimeerrors.1.Itallowssafeaccesstodeeplynestedobjectproperties,suchasuser.profile?.settings?.theme.2.Itenablescallingme

This article explores in-depth how to automatically generate solveable puzzles for the Double-Choco puzzle game. We will introduce an efficient data structure - a cell object based on a 2D grid that contains boundary information, color, and state. On this basis, we will elaborate on a recursive block recognition algorithm (similar to depth-first search) and how to integrate it into the iterative puzzle generation process to ensure that the generated puzzles meet the rules of the game and are solveable. The article will provide sample code and discuss key considerations and optimization strategies in the generation process.

The most common and recommended method for removing CSS classes from DOM elements using JavaScript is through the remove() method of the classList property. 1. Use element.classList.remove('className') to safely delete a single or multiple classes, and no error will be reported even if the class does not exist; 2. The alternative method is to directly operate the className property and remove the class by string replacement, but it is easy to cause problems due to inaccurate regular matching or improper space processing, so it is not recommended; 3. You can first judge whether the class exists and then delete it through element.classList.contains(), but it is usually not necessary; 4.classList

JavaScript's class syntax is syntactic sugar inherited by prototypes. 1. The class defined by class is essentially a function and methods are added to the prototype; 2. The instances look up methods through the prototype chain; 3. The static method belongs to the class itself; 4. Extends inherits through the prototype chain, and the underlying layer still uses the prototype mechanism. Class has not changed the essence of JavaScript prototype inheritance.

First, use npxstorybookinit to install and configure Storybook in the React project, run npmrunstorybook to start the local development server; 2. Organize component file structure according to functions or types, and create corresponding .stories.js files to define different states in each component directory; 3. Use Storybook's Args and Controls systems to achieve dynamic attribute adjustments to facilitate testing of various interactive states; 4. Use MDX files to write rich text documents containing design specifications, accessibility instructions, etc., and support MDX loading through configuration; 5. Define the design token through theme.js and use preview.js
