當我嘗試通過更改用戶mouseOver按鈕時的狀態來更改本節的樣式時,這很奇怪。 h1標籤的內容已更改,但款式不是。有人可以幫助我嗎?更改狀態但不改變樣式Reactjs
export class Jumbotron extends Component {
constructor (props) {
super(props)
this.state = {
style: {
backgroundImage: 'none'
}
}
this.handleSubjectMouseOver = this.handleSubjectMouseOver.bind(this)
}
handleSubjectMouseOver (bg) {
console.log(bg)
this.setState({
style: {
backgroundImage: bg
}
})
}
render() {
return <section className="jumbotron container" style=
{this.state.style}>
<h1>{this.state.style}</h1>
<div className="jumbotron-content">
<h2 style={{marginBottom: 3, marginTop: 40}}>Learning is fun with
LIVE</h2>
<div style={{marginLeft: 10}}>Body ........</div>
<div style={{marginBottom: 20, marginTop: 80}}
className="subjects-title">TOP CLASSES, TUTORIAL & TIPS TO EXCEL IN:
</div>
<div className="subjects" style={{backgroundImage: this.state.background}}>
<RaisedButton label="MATH" secondary onMouseMove={() => { this.handleSubjectMouseOver(mathBg) }} />
<RaisedButton label="PHYSICS" secondary onMouseMove={() => { this.handleSubjectMouseOver(physicBg) }} />
<RaisedButton label="CHEMISTRY" secondary onMouseMove={() => { this.handleSubjectMouseOver(chemistryBg) }} />
<RaisedButton label="BIOLOGY" secondary onMouseMove={() => { this.handleSubjectMouseOver(biologyBg) }} />
<RaisedButton label="COMP SCIENCE" secondary
onMouseMove={() => { this.handleSubjectMouseOver(computerBg) }} />
</div>
</div>
</section>
}
昨天我有這個問題,而不是直接改變CSS。讓你的CSS有兩個不同的類,在事件發生變化時,切換這些類。另外,確保你有檢查狀態變化的條件,你是否在你的代碼中這樣做? – Legolas
在我的情況下,我沒有任何有條件的渲染。因爲背景圖像的數量是動態的。我不能在這裏使用課程。 :( –
您能否發佈一些更詳細的代碼,以便我能夠理解問題所在? – Legolas