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.

Revisions

  1. jacob-beltran revised this gist Mar 6, 2017. No changes.
  2. jacob-beltran created this gist Mar 3, 2017.
    32 changes: 32 additions & 0 deletions fallbacks.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    /*
    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 }/>
    }