The first method: dynamically add a class to show or hide text by clicking a button as a demoimport 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 demoimport 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:
|
<<: Simple method to install mysql under linux
>>: Docker Data Storage Volumes Detailed Explanation
This article describes the MySQL integrity constr...
There are many tools, components and programs for...
Table of contents Preface The role of render Rend...
Table of contents 1. Get the file extension 2. Co...
Mysql-connector-java driver version problem Since...
This article example shares the specific code of ...
Recently, there have been many database-related o...
In the XHTML language, we all know that the ul ta...
calc is a function in CSS that is used to calcula...
Table of contents What is an event A Simple Examp...
Block-level element features : •Always occupies a ...
The reason for writing such an article is that on...
What is a selector? The role of the selector is t...
Win10 system locally installed MySQL8.0.20, perso...
Judgment symbols are often used in MySQL, and not...