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 { formatDateForViewHeader } from './format'
import { bleeding as labels } from './labels' import { bleeding as labels } from './labels'
import getCycleDay from './get-cycle-day' import getCycleDay from './get-cycle-day'
import { bleedingDaysSortedByDate } from './db'
export default class Bleeding extends Component { export default class Bleeding extends Component {
constructor(props) { constructor(props) {
+9 -4
View File
@@ -9,10 +9,11 @@ export default class DatePickView extends Component {
super(props) super(props)
this.state = { bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) } this.state = { bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) }
// so we rerender the calendar when there are new bleeding days bleedingDaysSortedByDate.addListener(setStateWithCalendarFormattedDays.bind(this))
bleedingDaysSortedByDate.addListener(() => { }
this.setState({ bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) })
}) componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(setStateWithCalendarFormattedDays)
} }
passDateToDayView(result) { passDateToDayView(result) {
@@ -44,3 +45,7 @@ function getBleedingDaysInCalFormat(bleedingDaysSortedByDate) {
return acc return acc
}, {}) }, {})
} }
function setStateWithCalendarFormattedDays() {
this.setState({ bleedingDaysInCalFormat: getBleedingDaysInCalFormat(bleedingDaysSortedByDate) })
}