Skip to content

Instantly share code, notes, and snippets.

@chiefGui
Created February 22, 2017 16:37
Show Gist options
  • Save chiefGui/9c6675f166e029ec5a29af5cf66321f4 to your computer and use it in GitHub Desktop.
Save chiefGui/9c6675f166e029ec5a29af5cf66321f4 to your computer and use it in GitHub Desktop.
import React from 'react'
class Lightbox extends Component {
constructor () {
super()
this.prev = this.prev.bind(this)
this.next = this.next.bind(this)
}
state = {currentImageUrl: null}
componentWillMount () {
const {currentImageUrl} = this.props
this.setState({currentImageUrl: currentImageUrl})
}
prev () {
const {images} = this.props
this.setState({currentImageUrl: images[0]})
}
next () {
const {images} = this.props
this.setState({currentImageUrl: images[2]})
}
render () {
const {currentImageUrl} = this.state
return (
<div>
<button onClick={this.prev}>Previous</button>
<img src={currentImageUrl} />
<button onNext={this.next}>Next</button>
</div>
)
}
}
class Home extends Component {
prevImageOnLightbox () {
this.setState({currentLightboxImage: IMAGES[0]})
}
nextImageOnLightbox () {
this.setState({currentLightboxImage: IMAGES[2]})
}
render () {
const IMAGES = [1, 2, 3]
return (
<Lightbox
currentImageUrl={IMAGES[1]}
images={IMAGES}
onPrev={this.prevImageOnLightbox}
onNext={this.nextImageOnLightbox}
/>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment