Im試圖顯示React中的一個或另一個組件。點擊時我可以顯示一個組件,但我無法顯示其他組件。顯示組件是否爲true,否則顯示其他組件(在React中)
陣營代碼:
import React, { Component } from 'react'
import { Motion, spring } from 'react-motion'
import Header from '../../components/services/Header'
import ServiceSelector from '../../components/services/ServiceSelector'
import PriceCalculator from '../../components/services/PriceCalculator'
import VVSRequest from '../../components/services/VvsRequest'
import VVSFeatures from '../../components/services/VVSFeatures'
import GalleryPreview from '../../components/services/GalleryPreview'
import Footer from '../../components/services/Footer'
require('styles/_servicesPage/services.css')
require('styles/_servicesPage/serviceSelector.css')
var ReactCSSTransitionGroup = require('react-addons-css-transition-group');
export default class Services extends Component {
componentWillMount() {
this.state = {
showPriceCalculator: false
}
}
handleClick(e) {
const userChoice = e.target.className
this.setState({ userChoice })
}
toggleDiv(toggle) {
console.log('te',toggle);
this.setState({showPriceCalculator: toggle})
}
render() {
var styleVar = {
backgroundImage: 'url(assets/images/services/service_bg.jpg)'
}
return (
<div>
<Header />
<ServiceSelector toggleDiv={this.toggleDiv.bind(this)}/>
{this.state.showPriceCalculator ?
<PriceCalculator toggleDiv={this.toggleDiv.bind(this)}/> : <VVSFeatures />}
{/*<VVSFeatures />
<VVSRequest />*/}
<GalleryPreview />
<Footer />
</div>
)
}
}
在負載它顯示了
<VVSFeatures />
,因爲它應該
但狀態被設置爲
showPriceCalculator: true
然後後是再次設置爲false,noth發生(應該再次出現)
它以正確的狀態值恢復,但沒有任何反應。 什麼可能是錯的?
謝謝!
你100%肯定的狀態是正確的?另外,我不知道你在哪裏使用'handleClick',但'this.setState({userChoice})'可能會覆蓋你的狀態。也許在渲染內部放置一個console.log的狀態,這樣你就可以在每一點看到它來仔細檢查?代碼的哪部分將其設置爲false? – ajmajmajma
你怎麼知道'它以正確的狀態退縮'?你可以在'render(){'之後做'console.log('showPriceCalculator:',this.state.showPriceCalculator)',讓我們知道你看到了正確的值嗎? –
是的,我在渲染方法內註銷了狀態,當我從ServiceSelector將其設置爲true時,我打印出true,並且當我將它從PriceCalculator組件設置爲false時,它將打印爲false。 – AlfredO