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)
}
cssclass属性render() {
const display2 = {
display:this.state.display2 ? 'block' : 'none'
}
return (
{/*动态添加⼀个style来改变样式*/}
你是我的唯⼀
点击隐藏2
点击显⽰2
)
}
handleshow2() {
this.setState({
display2:true
})
}
handlehide2() {
this.setState({
display2:false
})
}
}
export default Demo;
总结:⽤class来改变css样式,可以写多个动态改变的css属性,看起不杂乱,⽽⽤style写的话,如果写多个css属性就会看起复杂。都是个⼈观点,不⾜请指出
到此这篇关于详解react的两种动态改变css样式的⽅法的⽂章就介绍到这了,更多相关react动态改变css样式内容请搜索脚本之家以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持脚本之家!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。