Remove listeners on layout before re-adding them
This commit is contained in:
+27
-10
@@ -48,9 +48,7 @@ export default class CycleChart extends Component {
|
|||||||
onLayout = ({ nativeEvent }) => {
|
onLayout = ({ nativeEvent }) => {
|
||||||
if (this.state.chartHeight) return
|
if (this.state.chartHeight) return
|
||||||
const height = nativeEvent.layout.height
|
const height = nativeEvent.layout.height
|
||||||
this.setState({ chartHeight: height })
|
const reCalculateChartInfo = () => {
|
||||||
this.reCalculateChartInfo = (_, changes) => {
|
|
||||||
if (nothingChanged(changes)) return
|
|
||||||
// how many symptoms need to be displayed on the chart's upper symptom row?
|
// how many symptoms need to be displayed on the chart's upper symptom row?
|
||||||
this.symptomRowSymptoms = [
|
this.symptomRowSymptoms = [
|
||||||
'bleeding',
|
'bleeding',
|
||||||
@@ -66,8 +64,8 @@ export default class CycleChart extends Component {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
this.xAxisHeight = this.state.chartHeight * config.xAxisHeightPercentage
|
this.xAxisHeight = height * config.xAxisHeightPercentage
|
||||||
const remainingHeight = this.state.chartHeight - this.xAxisHeight
|
const remainingHeight = height - this.xAxisHeight
|
||||||
this.symptomHeight = config.symptomHeightPercentage * remainingHeight
|
this.symptomHeight = config.symptomHeightPercentage * remainingHeight
|
||||||
this.symptomRowHeight = this.symptomRowSymptoms.length *
|
this.symptomRowHeight = this.symptomRowSymptoms.length *
|
||||||
this.symptomHeight
|
this.symptomHeight
|
||||||
@@ -78,16 +76,35 @@ export default class CycleChart extends Component {
|
|||||||
this.chartSymptoms.push('temperature')
|
this.chartSymptoms.push('temperature')
|
||||||
}
|
}
|
||||||
|
|
||||||
const columnData = this.makeColumnInfo()
|
const columnData = this.makeColumnInfo(nfpLines(), this.chartSymptoms)
|
||||||
this.setState({ columns: columnData })
|
this.setState({
|
||||||
|
columns: columnData,
|
||||||
|
chartHeight: height
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cycleDaysSortedByDate.addListener(this.reCalculateChartInfo)
|
reCalculateChartInfo()
|
||||||
this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false)
|
this.updateListeners(reCalculateChartInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
updateListeners(dataUpdateHandler) {
|
||||||
|
// remove existing listeners
|
||||||
|
if(this.handleDbChange) {
|
||||||
|
this.cycleDaysSortedByDate.removeListener(this.handleDbChange)
|
||||||
|
}
|
||||||
|
if (this.removeObvListener) this.removeObvListener()
|
||||||
|
|
||||||
|
this.handleDbChange = (_, changes) => {
|
||||||
|
if (nothingChanged(changes)) return
|
||||||
|
dataUpdateHandler()
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cycleDaysSortedByDate.addListener(this.handleDbChange)
|
||||||
|
this.removeObvListener = scaleObservable(dataUpdateHandler, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
this.cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo)
|
this.cycleDaysSortedByDate.removeListener(this.handleDbChange)
|
||||||
this.removeObvListener()
|
this.removeObvListener()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user