Last active
March 6, 2017 19:50
-
-
Save jacob-beltran/ae63069d95831fb49e59e74eb7ddbd11 to your computer and use it in GitHub Desktop.
React Performance: Fallback Values Example
This file contains 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
/* | |
Sometimes a fallback value or object may be created in the render function | |
( or prop value ) to avoid undefined value errors. In these cases, it's best | |
to define the fallbacks as a constant external to the component instead of | |
creating a new literal. | |
/* | |
/* Bad */ | |
render() { | |
let thingys = []; | |
// If this.props.thingys is not defined a new array literal is created above. | |
if( this.props.thingys ) { | |
thingys = this.props.thingys; | |
} | |
return <ThingyHandler thingys={ thingys }/> | |
} | |
/* Bad */ | |
render() { | |
// This has functionally the same outcome as the above example. | |
return <ThingyHandler thingys={ this.props.thingys || [] }/> | |
} | |
/* Good */ | |
// Declare outside of component | |
const NO_THINGYS = []; | |
render() { | |
return <ThingyHandler thingys={ this.props.thingys || NO_THINGYS }/> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment