Created
August 23, 2016 13:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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