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
Table of contents Basic database operations 2) Vi...
Problem explanation: When using the CSS animation...
Look at the solution first #------------The probl...
In MySQL, we often use order by for sorting and l...
Table of contents Purpose Experimental environmen...
This article shares the specific implementation c...
Recently, I have a project that requires using ifr...
Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...
1. Prerequisites JDK has been installed echo $PAT...
Modify the group to which a user belongs in Linux...
Recently, I encountered a requirement to display ...
During the development process, we often use the ...
Table of contents Overview 1. Download via URL 2....
Solution to the data asymmetry problem between My...
Preface When it comes to database transactions, a...