How to change css style in react

藏色散人
Release: 2022-12-30 10:02:53
Original
2033 people have browsed it
How to change css style in react: 1. Dynamically add a class to change the style, the code is like "

"; 2. Dynamically add a style to change the style, with code such as "

".

How to change css style in react

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

How to change css style in react?

Two ways to dynamically change css styles in react

The first one: dynamically add a class to click the button to display and hide the text as demo

import React, { Component, Fragment } from 'react';
import './style.css';
class Demo extends Component{
    constructor(props) {
        super(props);
        this.state = {
            display: true
        }
        this.handleshow = this.handleshow.bind(this)
        this.handlehide = this.handlehide.bind(this)
    }
    render() {
        return (
            <Fragment>
                {/*动态添加一个class来改变样式*/}
                <p className={this.state.display?"active":"active1"}>你是我的唯一</p>
                <button onClick={this.handlehide}>点击隐藏</button>
                <button onClick={this.handleshow}>点击显示</button>
            </Fragment>
        )
    }
    handleshow() {
        this.setState({
            display:true
        })
    }
    handlehide() {
        this.setState({
            display:false
        })
    }
}
export default Demo;
Copy after login

css Code:

 .active{
      display: block;
  }
  .active1{
    display: none;
  }
Copy after login

Second: add a style dynamically, click the button to display and hide the text as demo

import React, { Component, Fragment } from &#39;react&#39;;
class Demo extends Component{
    constructor(props) {
        super(props);
        this.state = {
            display2: true
        }
        this.handleshow2 = this.handleshow2.bind(this)
        this.handlehide2 = this.handlehide2.bind(this)
    }
    render() {
        const display2 = {
            display:this.state.display2 ? &#39;block&#39; : &#39;none&#39;
        }
        return (
            <Fragment>
                 {/*动态添加一个style来改变样式*/}
                 <p style={display2}>你是我的唯一</p>
                <button onClick={this.handlehide2}>点击隐藏2</button>
                <button onClick={this.handleshow2}>点击显示2</button>
            </Fragment>
        )
    }
    handleshow2() {
        this.setState({
            display2:true
        })
    }
    handlehide2() {
        this.setState({
            display2:false
        })
    }
}
export default Demo;
Copy after login

Summary: use class to change css style, you can write multiple dynamic changes CSS attributes don’t look messy, but when written in style, writing multiple CSS attributes will look complicated. These are all personal opinions, please point out any shortcomings

Recommended study: "react video tutorial"

The above is the detailed content of How to change css style in react. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!