Created
November 1, 2018 15:48
-
-
Save sebvilhelm/2b87c4dd00b9e2e074ab346c2c2f80fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { tns } from 'tiny-slider/src/tiny-slider.module' | |
export default class Carousel extends Component { | |
constructor(props) { | |
super(props) | |
this.sliderContainer = React.createRef() | |
} | |
componentDidMount() { | |
this.slider = tns({ | |
...this.props, | |
container: this.sliderContainer.current, | |
}) | |
} | |
componentWillUnmount() { | |
if (this.slider) { | |
this.slider.destroy() | |
} | |
} | |
goToSlide = index => { | |
this.slider.goTo(index) | |
} | |
render() { | |
return ( | |
<div ref={this.sliderContainer}> | |
{React.Children.map(this.props.children, (child, index) => | |
React.cloneElement(child, { onClick: () => this.goToSlide(index) }) | |
)} | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment