From 88c84482f8ac2f7c3fd07229cadc0db878a1aa8f Mon Sep 17 00:00:00 2001 From: alesanderlopez Date: Tue, 31 Jul 2018 00:12:45 +0100 Subject: [PATCH 1/3] Update Counter.js - added method: [reset] to reset the counter again to start. - added method: [componentWillUnmount] to stop the counter and fix memory leak when Unmount parent view or change view in react-native-router-flux - update method: [animate] for stop the loop when the componentWillUnmount was executed --- src/Counter.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Counter.js b/src/Counter.js index 7599465..3160912 100644 --- a/src/Counter.js +++ b/src/Counter.js @@ -26,9 +26,22 @@ export default class Counter extends Component { componentDidMount() { this.startTime = Date.now(); requestAnimationFrame(this.animate.bind(this)); + this.umount = false + } + + 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) { From 406521c570f0cd902103f4a54c4719e81629977e Mon Sep 17 00:00:00 2001 From: alesanderlopez Date: Tue, 31 Jul 2018 00:13:42 +0100 Subject: [PATCH 2/3] Update Counter.js --- src/Counter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Counter.js b/src/Counter.js index 3160912..b3937da 100644 --- a/src/Counter.js +++ b/src/Counter.js @@ -25,8 +25,8 @@ export default class Counter extends Component { componentDidMount() { this.startTime = Date.now(); - requestAnimationFrame(this.animate.bind(this)); this.umount = false + requestAnimationFrame(this.animate.bind(this)); } componentWillUnmount() { From f5b7bab1b0731241d913365f25517e465a77b091 Mon Sep 17 00:00:00 2001 From: alesanderlopez Date: Tue, 31 Jul 2018 00:17:01 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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