Remove listener on componentUnmount and clean up state
This commit is contained in:
+14
-2
@@ -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') {
|
||||||
@@ -44,3 +50,9 @@ export default class DayView extends Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setStateWithCurrentCycleDayNumber() {
|
||||||
|
this.setState({
|
||||||
|
cycleDayNumber: getCycleDay(this.cycleDay.date)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user