From f98a60192b5cc0b25d8f13f6389c779b8cc61141 Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Sat, 9 Jun 2018 15:00:12 +0200
Subject: [PATCH] Remove listener on componentUnmount and clean up state

---
 day-view.js | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/day-view.js b/day-view.js
index b615ffd4..7ed99289 100644
--- a/day-view.js
+++ b/day-view.js
@@ -15,14 +15,20 @@ const getCycleDay = cycleDayModule(bleedingDaysSortedByDate)
 export default class DayView extends Component {
   constructor(props) {
     super(props)
+    this.cycleDay = props.navigation.state.params.cycleDay
     this.state = {
-      cycleDay: props.navigation.state.params.cycleDay
+      cycleDayNumber: getCycleDay(this.cycleDay.date),
     }
+    bleedingDaysSortedByDate.addListener(setStateWithCurrentCycleDayNumber.bind(this))
+  }
+
+  componentWillUnmount() {
+    bleedingDaysSortedByDate.removeListener(setStateWithCurrentCycleDayNumber)
   }
 
   render() {
     const navigate = this.props.navigation.navigate
-    const day = this.state.cycleDay
+    const day = this.cycleDay
     const bleedingValue = day.bleeding && day.bleeding.value
     let bleedingLabel
     if (typeof bleedingValue === 'number') {
@@ -43,4 +49,10 @@ export default class DayView extends Component {
       </View >
     )
   }
+}
+
+function setStateWithCurrentCycleDayNumber() {
+  this.setState({
+    cycleDayNumber: getCycleDay(this.cycleDay.date)
+  })
 }
\ No newline at end of file
-- 
GitLab