diff --git a/README.md b/README.md index 8894bf7..8daada9 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,22 @@ import Counter from 'react-native-counter'; /> ``` +# Reset from parent view + +```javascript +import Counter from 'react-native-counter'; + + this.counter = counter} +/> + + // Example method restarting the counter + resetCounter() { + this.counter.reset(); + } +``` + The easing prop is a string corresponding to one of any function from [eases](https://github.com/mattdesl/eases). # License diff --git a/src/Counter.js b/src/Counter.js index 7599465..b3937da 100644 --- a/src/Counter.js +++ b/src/Counter.js @@ -25,10 +25,23 @@ export default class Counter extends Component { componentDidMount() { this.startTime = Date.now(); + this.umount = false requestAnimationFrame(this.animate.bind(this)); } + componentWillUnmount() { + this.umount = true + } + + reset() { + this.startTime = Date.now(); + this.stop = false + requestAnimationFrame(this.animate.bind(this)); + } + animate() { + if(this.umount) return; + const { onComplete } = this.props; if (this.stop) {