Skip to content

Instantly share code, notes, and snippets.

@streamich
Last active March 5, 2018 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streamich/68b4470a8365f7b7ac6b4f614cdf45d2 to your computer and use it in GitHub Desktop.
Save streamich/68b4470a8365f7b7ac6b4f614cdf45d2 to your computer and use it in GitHub Desktop.
Usage cases for async generator render functions: https://github.com/reactjs/rfcs/issues/29

Load data on client and server:

async function * MyComponent() {
  yield (<div>Loading...</div>);
  const users = await fetch('/users');
  yield () =>
    <div>
      <UserList users={users} />
      <button onClick={this.props.onClick}>{this.props.buttonLabel}</button>
    </div>;
}

Load data in steps:

async function * MyComponent() {
  yield (<div>Loading...</div>);
  let users = await fetch('/users/1');
  yield () => <UserList users={users} />;
  users = [...users, ...(await fetch('/users/2'))];
  yield () => <UserList users={users} />;
}

Code splitting:

async function * MyComponent() {
  yield (<div>Loading...</div>);
  const {LoadedComponent} = await import('./LoadedComponent');
  yield () => <LoadedComponent {...this.props} />;
}

Stream of events:

async function * EventMonitor() {
  for await (const event of this.props.stream)
    yield () => <EventInfo event={event} />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment