Remove listener when component unmounts

This commit is contained in:
Julia Friesel
2018-06-09 15:06:13 +02:00
parent 80bf63adc2
commit 75a97089f9
2 changed files with 9 additions and 5 deletions
-1
View File
@@ -11,7 +11,6 @@ import { saveBleeding } from './db'
import { formatDateForViewHeader } from './format'
import { bleeding as labels } from './labels'
import getCycleDay from './get-cycle-day'
import { bleedingDaysSortedByDate } from './db'
export default class Bleeding extends Component {
constructor(props) {
+9 -4
View File
@@ -9,10 +9,11 @@ export default class DatePickView extends Component {
super(props)
this.state = { bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) }
// so we rerender the calendar when there are new bleeding days
bleedingDaysSortedByDate.addListener(() => {
this.setState({ bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) })
})
bleedingDaysSortedByDate.addListener(setStateWithCalendarFormattedDays.bind(this))
}
componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(setStateWithCalendarFormattedDays)
}
passDateToDayView(result) {
@@ -44,3 +45,7 @@ function getBleedingDaysInCalFormat(bleedingDaysSortedByDate) {
return acc
}, {})
}
function setStateWithCalendarFormattedDays() {
this.setState({ bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) })
}