Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including await-component with all npm packages installed. Try it out:

require("react/package.json"); // react is a peer dependency. var awaitComponent = require("await-component")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

await-component v0.1.1

await-component

NPM version

Code-splitting with import() and loading boundaries

Features

  • Supports any Promise based code-splitting API import(), require.ensure(), etc
  • Display a custom loading component while waiting for sub-components to load.
  • Handle timeouts and errors with a custom error component.
  • Adds delay to prevent Flash of Loading Content
  • Preload components with preload

Install

yarn add await-component
import {Await, Async, preload} from 'await-component';

Examples

Using Async(() => <Promise>)

import React from 'react';
import {Await, Async} from 'await-component';

const Container = Async(() => import('./Container'));
const Sum = Async(() => import('./Sum'));

export default () => (
  <Await loading={<div>Loading...</div>} error={<div>ERROR!</div>}>
    <Container>
      <Sum a={1} b={2} />
      <Sum a={2} b={2} />
      <Sum a={5} b={5} />
    </Container>
  </Await>
);

Preloading

import React from 'react';
import {Await, Async, preload} from 'await-component';

const Container = Async(() => import('./Container'));
const Sum = Async(() => import('./Sum'));

preload(Container);

export default () => (
  <Await loading={<div>Loading...</div>} error={<div>ERROR!</div>}>
    <Container>
      <Sum a={1} b={2} />
      <Sum a={2} b={2} />
      <Sum a={5} b={5} />
    </Container>
  </Await>
);

Using JSX Pragma (experimental)

/* @jsx Async.createElement */
import React from 'react';
import {Await, Async} from 'await-component';

const Container = import('./Container');
const Sum = import('./Sum');

export default () => (
  <Await loading={<div>Loading...</div>} error={<div>ERROR!</div>}>
    <Container>
      <Sum a={1} b={2} />
      <Sum a={2} b={2} />
      <Sum a={5} b={5} />
    </Container>
  </Await>
);

Changelog

See the Changelog

Contributing

See the Contributors Guide

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free