Detailed explanation of two ways to dynamically change CSS styles in react

Detailed explanation of two ways to dynamically change CSS styles in react

The first method: dynamically add a class to show or hide text by clicking a button as a 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>
                {/*Dynamically add a class to change the style*/}
                <p className={this.state.display?"active":"active1"}>You are my only one</p>
                <button onClick={this.handlehide}>Click to hide</button>
                <button onClick={this.handleshow}>Click to show</button>
            </Fragment>
        )
    }
    handleshow() {
        this.setState({
            display:true
        })
    }
    handlehide() {
        this.setState({
            display:false
        })
    }
}
export default Demo;

CSS code:

 .active{
      display: block;
  }
  .active1{
    display: none;
  }

The second method: dynamically add a style to show and hide text by clicking a button as a demo

import React, { Component, Fragment } from 'react';
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 ? 'block' : 'none'
        }
        return (
            <Fragment>
                 {/*Dynamically add a style to change the style*/}
                 <p style={display2}>You are my only one</p>
                <button onClick={this.handlehide2}>Click to hide 2</button>
                <button onClick={this.handleshow2}>Click to show 2</button>
            </Fragment>
        )
    }
    handleshow2() {
        this.setState({
            display2:true
        })
    }
    handlehide2() {
        this.setState({
            display2:false
        })
    }
}
export default Demo;

Summary: Using class to change the CSS style, you can write multiple dynamically changing CSS attributes, which looks uncluttered. If you use style to write multiple CSS attributes, it will look complicated. These are all personal opinions, please point out any deficiencies

This concludes this article on two ways to dynamically change CSS styles in React. For more information about dynamically changing CSS styles in React, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • React uses emotion to write CSS code
  • Solution to CSS reference path error based on react project packaging
  • React uses CSS to implement react animation function example
  • 7 ways to use CSS in React (the most complete summary)
  • An example of how to use CSS Modules in Create React App
  • Example code for using css modules in create-react-app
  • Detailed explanation of adding css modules, sasss and antd using create-react-app
  • ReactJs method of setting css style
  • How to write CSS elegantly with react

<<:  Simple method to install mysql under linux

>>:  Docker Data Storage Volumes Detailed Explanation

Recommend

Summary of basic knowledge points of MySql database

Table of contents Basic database operations 2) Vi...

Detailed explanation of the pitfalls of mixing MySQL order by and limit

In MySQL, we often use order by for sorting and l...

Introduction to Apache deployment of https in cryptography

Table of contents Purpose Experimental environmen...

Vue implements adding watermark to uploaded pictures

This article shares the specific implementation c...

A brief analysis of whether using iframe to call a page will cache the page

Recently, I have a project that requires using ifr...

Detailed graphic and text instructions for installing MySQL 5.7.20 on Mac OS

Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...

Detailed instructions for installing Jenkins on Ubuntu 16.04

1. Prerequisites JDK has been installed echo $PAT...

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

Example code for implementing background transparency and opaque text with CSS3

Recently, I encountered a requirement to display ...

Notes on using $refs in Vue instances

During the development process, we often use the ...

How to download excel stream files and set download file name in vue

Table of contents Overview 1. Download via URL 2....

Solution to the data asymmetry problem between MySQL and Elasticsearch

Solution to the data asymmetry problem between My...