Skip to content

Commit

Permalink
Defer Flatpickr initialization
Browse files Browse the repository at this point in the history
Profiling shows that Flatpickr takes a non-trivial amount of time to initialize, so defer to the next available frame after mount.

#performance
  • Loading branch information
aduth committed May 27, 2017
1 parent 5bf76c9 commit eb8f8b8
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/components/date-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ export default class DatePicker extends Component {
};

componentDidMount() {
// Profiling shows that Flatpickr takes a non-trivial amount of time to
// initialize, so let's defer it to the next available frame.
if ( window.requestAnimationFrame ) {
this.scheduledInitialize = window.requestAnimationFrame( this.initialize );
} else {
this.initialize();
}
}

componentWillReceiveProps( nextProps ) {
if ( this.flatpickr && nextProps.value ) {
this.flatpickr.setDate( nextProps.value );
}
}

componentWillUnmount() {
if ( this.flatpickr ) {
this.flatpickr.destroy();
} else {
window.cancelAnimationFrame( this.initialize );
}
}

shouldComponentUpdate() {
return false;
}

initialize = () => {
this.flatpickr = new Flatpickr( this.input, {
onChange: this.props.onChange
} );
Expand All @@ -34,21 +62,7 @@ export default class DatePicker extends Component {
}

this.flatpickr.redraw();
}

componentWillReceiveProps( nextProps ) {
if ( nextProps.value ) {
this.flatpickr.setDate( nextProps.value );
}
}

componentWillUnmount() {
this.flatpickr.destroy();
}

shouldComponentUpdate() {
return false;
}
};

setInputRef = ( input ) => {
this.input = input;
Expand Down

0 comments on commit eb8f8b8

Please sign in to comment.