class ProductCarousel extends Component {
constructor (props) {
super(props)
this.state = {
active: -1,
fullScreen: false
}
this.flky = {}
}
componentDidMount (props) {
this.flky = new Flickity('.carousel', flickityOptions)
this.flky.on('select',() => {
this.setState({ active: this.flky.selectedIndex })
})
}
fullScreen() {
this.setState({ fullScreen: true })
console.log(this.state)
}
render() {
const images = this.props.images
return (
<div>
<div className='carousel'>
{images.map((url, i) => (
<img
className='productPhoto'
key={i.toString()}
onClick={this.fullScreen.bind(this)}
src={stripUrl(url)}
style={{maxWidth: '100%'}}
/>
))}
</div>
</div>
)
}
}
私はこのコンポーネントをjs内に持っていますが、関数fullScreenではthis.state.fullScree
nが真であると思いますが、それでもなぜ偽ですか?どのように私の状態を更新できないのですか?
OKを超える、状態更新は、ページの再レンダリングがトリガされます(私はこの関数内で他に何もする必要はありません。ページの別の部分に状態が必要です) – Himmators
はい、setstateは処理されたときに再レンダリングを行います。 – Tannin