Skip to content

Instantly share code, notes, and snippets.

@aykutyaman
Created August 23, 2016 13:30
Show Gist options
  • Save aykutyaman/4397076572a575c710b145a01bece03f to your computer and use it in GitHub Desktop.
Save aykutyaman/4397076572a575c710b145a01bece03f to your computer and use it in GitHub Desktop.
This goes against the "single source of state" principle, but if computations on a prop are expensive you can cache them on the component. Instead of directly using doExpensiveComputation(this.prop.someProp) in the render method, we can wrap the call that caches the value if the prop is unchanged
getCachedExpensiveComputation() {
if (this._cachedSomeProp !== this.prop.someProp) {
this._cachedSomeProp = this.prop.someProp;
this._cachedComputation = doExpensiveComputation(this.prop.someProp);
}
return this._cachedComputation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment