本篇文章為大家展示了使用react怎么動態改變css樣式,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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>
{/*動態添加一個class來改變樣式*/}
<p className={this.state.display?"active":"active1"}>你是我的唯一</p>
<button onClick={this.handlehide}>點擊隱藏</button>
<button onClick={this.handleshow}>點擊顯示</button>
</Fragment>
)
}
handleshow() {
this.setState({
display:true
})
}
handlehide() {
this.setState({
display:false
})
}
}
export default Demo;css代碼:
.active{
display: block;
}
.active1{
display: none;
}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>
{/*動態添加一個style來改變樣式*/}
<p style={display2}>你是我的唯一</p>
<button onClick={this.handlehide2}>點擊隱藏2</button>
<button onClick={this.handleshow2}>點擊顯示2</button>
</Fragment>
)
}
handleshow2() {
this.setState({
display2:true
})
}
handlehide2() {
this.setState({
display2:false
})
}
}
export default Demo;上述內容就是使用react怎么動態改變css樣式,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。