Skip to content

Instantly share code, notes, and snippets.

@jacob-beltran
Forked from sieverk/deferComponentRender.js
Created April 12, 2017 16:57
Show Gist options
  • Save jacob-beltran/efc9633cafb606c43e125c77a2669b10 to your computer and use it in GitHub Desktop.
Save jacob-beltran/efc9633cafb606c43e125c77a2669b10 to your computer and use it in GitHub Desktop.

Revisions

  1. @sieverk sieverk created this gist Apr 6, 2017.
    27 changes: 27 additions & 0 deletions deferComponentRender.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    import hoistStatics from 'hoist-non-react-statics';
    import React from 'react';

    /**
    * Allows two animation frames to complete to allow other components to update
    * and re-render before mounting and rendering an expensive `WrappedComponent`.
    */
    export default function deferComponentRender(WrappedComponent) {
    class DeferredRenderWrapper extends React.Component {
    constructor(props, context) {
    super(props, context);
    this.state = { shouldRender: true };
    }

    componentDidMount() {
    window.requestAnimationFrame(() => {
    window.requestAnimationFrame(() => this.setState({ shouldRender: true }));
    });
    }

    render() {
    return this.state.shouldRender ? <WrappedComponent {...this.props} /> : null;
    }
    }

    return hoistStatics(DeferredRenderWrapper, WrappedComponent);
    }