Remove listener on componentUnmount and clean up state

This commit is contained in:
Julia Friesel
2018-06-09 15:00:12 +02:00
parent a56ee7df38
commit f98a60192b
+14 -2
View File
@@ -15,14 +15,20 @@ const getCycleDay = cycleDayModule(bleedingDaysSortedByDate)
export default class DayView extends Component { export default class DayView extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.cycleDay = props.navigation.state.params.cycleDay
this.state = { this.state = {
cycleDay: props.navigation.state.params.cycleDay cycleDayNumber: getCycleDay(this.cycleDay.date),
} }
bleedingDaysSortedByDate.addListener(setStateWithCurrentCycleDayNumber.bind(this))
}
componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(setStateWithCurrentCycleDayNumber)
} }
render() { render() {
const navigate = this.props.navigation.navigate const navigate = this.props.navigation.navigate
const day = this.state.cycleDay const day = this.cycleDay
const bleedingValue = day.bleeding && day.bleeding.value const bleedingValue = day.bleeding && day.bleeding.value
let bleedingLabel let bleedingLabel
if (typeof bleedingValue === 'number') { if (typeof bleedingValue === 'number') {
@@ -43,4 +49,10 @@ export default class DayView extends Component {
</View > </View >
) )
} }
}
function setStateWithCurrentCycleDayNumber() {
this.setState({
cycleDayNumber: getCycleDay(this.cycleDay.date)
})
} }