react改变css样式的方法:1、动态添加class,代码如“handleshow() {this.setstate({display:true})}...”;2、动态添加一个style,代码如“class demo extends component{...}”。

本教程操作环境:windows10系统、react18版、dell g3电脑。
react怎么改变css样式??
react的两种动态改变css样式的方法
第一种:动态添加class,以点击按钮让文字显示隐藏为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 (
{/*动态添加一个class来改变样式*/}
你是我的唯一
)
}
handleshow() {
this.setstate({
display:true
})
}
handlehide() {
this.setstate({
display:false
})
}
}
export default demo;css代码:
.active{
display: block;
}
.active1{
display: none;
}第二种:动态添加一个style,以点击按钮让文字显示隐藏为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 (
{/*动态添加一个style来改变样式*/}
你是我的唯一
)
}
handleshow2() {
this.setstate({
display2:true
})
}
handlehide2() {
this.setstate({
display2:false
})
}
}
export default demo;总结:用class来改变css样式,可以写多个动态改变的css属性,看起不杂乱,而用style写的话,如果写多个css属性就会看起复杂。都是个人观点,不足请指出
阿姆斯特郎回旋加速喷气式阿姆斯特郎炮