The class example is misleading in terms of the line of code. The first example would have the componentDidMount and componentDidUpdate to call the same function
componentDidMount() {
const newData = getDataWithinRange(this.props.dateRange)
this.setState({data: newData})
}
componentDidUpdate(prevProps) {
if (prevProps.dateRange != this.props.dateRange) {
const newData = getDataWithinRange(this.props.dateRange)
this.setState({data: newData})
}
}
Could be: componentDidMount() {
get(this.props.dateRange);
}
componentDidUpdate(prevProps) {
if (prevProps.dateRange != this.props.dateRange) {
get(this.props.dateRange);
}
}
get(dateRange){
const newData = getDataWithinRange(dateRange)
this.setState({data: newData})
}
It removes the duplication.