Skip to content

Instantly share code, notes, and snippets.

@jacob-beltran
Last active March 6, 2017 19:50
Show Gist options
  • Save jacob-beltran/ae63069d95831fb49e59e74eb7ddbd11 to your computer and use it in GitHub Desktop.
Save jacob-beltran/ae63069d95831fb49e59e74eb7ddbd11 to your computer and use it in GitHub Desktop.
React Performance: Fallback Values Example
/*
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