All Projects â†’ transitive-bullshit â†’ React Suspense Polyfill

transitive-bullshit / React Suspense Polyfill

Polyfill for the React Suspense API 😮

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Suspense Polyfill

Es5 Polyfill
ECMAScript 5 Polyfill for IE-8
Stars: ✭ 31 (-68.69%)
Mutual labels:  polyfill
Scroll Behavior Polyfill
A polyfill for the 'scroll-behavior' CSS-property
Stars: ✭ 76 (-23.23%)
Mutual labels:  polyfill
Polyfill
PHP polyfills
Stars: ✭ 1,333 (+1246.46%)
Mutual labels:  polyfill
Bootstrap Ie11
Internet Explorer 11 compatibility solution for Bootstrap 5
Stars: ✭ 47 (-52.53%)
Mutual labels:  polyfill
Web Bluetooth Polyfill
Windows 10 Web Bluetooth Polyfill
Stars: ✭ 68 (-31.31%)
Mutual labels:  polyfill
Polyfill Php54
This component provides functions unavailable in releases prior to PHP 5.4.
Stars: ✭ 93 (-6.06%)
Mutual labels:  polyfill
Ponyfill
🦄 Like polyfill but with pony pureness
Stars: ✭ 945 (+854.55%)
Mutual labels:  polyfill
Webcrypto Liner
webcrypto-liner is a polyfill that let's down-level User Agents (like IE/Edge) use libraries that depend on WebCrypto. (Keywords: Javascript, WebCrypto, Shim, Polyfill)
Stars: ✭ 98 (-1.01%)
Mutual labels:  polyfill
Css Vars Ponyfill
Client-side support for CSS custom properties (aka "CSS variables") in legacy and modern browsers
Stars: ✭ 1,166 (+1077.78%)
Mutual labels:  polyfill
Proposal Array Unique
ECMAScript proposal for Deduplicating method of Array
Stars: ✭ 96 (-3.03%)
Mutual labels:  polyfill
Random compat
PHP 5.x support for random_bytes() and random_int()
Stars: ✭ 7,950 (+7930.3%)
Mutual labels:  polyfill
Document Register Element
A stand-alone working lightweight version of the W3C Custom Elements specification
Stars: ✭ 1,123 (+1034.34%)
Mutual labels:  polyfill
Date Time Format Timezone
Surgically polyfills timezone support in Intl.DateTimeFormat API
Stars: ✭ 94 (-5.05%)
Mutual labels:  polyfill
Html Modules Toolkit
Transforming HTML standards of the future into JavaScript standards of the past
Stars: ✭ 45 (-54.55%)
Mutual labels:  polyfill
Webvr Polyfill
Use WebVR today, without requiring a special browser build.
Stars: ✭ 1,343 (+1256.57%)
Mutual labels:  polyfill
Proxy Polyfill
Proxy object polyfill
Stars: ✭ 951 (+860.61%)
Mutual labels:  polyfill
Webpack Polyfill Injector
Webpack plugin to automatically inject polyfills into your bundle without affecting modern browsers.
Stars: ✭ 84 (-15.15%)
Mutual labels:  polyfill
Es6 Promise Polyfill
ES6 Promise polyfill
Stars: ✭ 99 (+0%)
Mutual labels:  polyfill
Polyfill
Polyfill implements newer Ruby features into older versions.
Stars: ✭ 98 (-1.01%)
Mutual labels:  polyfill
Match Media
Universal polyfill for match media API using Expo APIs on mobile
Stars: ✭ 95 (-4.04%)
Mutual labels:  polyfill

react-suspense-polyfill

Provides a basic polyfill for the upcoming React Suspense APIs.

NPM Build Status JavaScript Style Guide

Status

This module is intended for understanding and experimenting with the upcoming React Suspense APIs.

Note that the actual version of Suspense that will ship with React is significantly more complicated and efficient than the version in this polyfill. It is meant solely for experimental purposes and to ease the burden of incremental upgrades.

How It Works

At its core, React Suspense works by allowing an async component to throw a Promise from its render method.

This polyfill mimics React's internal support for this behavior by implementing an error boundary in the Timeout component. If the error boundary encounters a thrown Promise, it waits until that Promise resolves and then attempts to re-render its children. It also handles falling back to loading content if the Promise takes too long to resolve.

The reason this polyfill does not support React v15 is because error boundaries weren't properly supported until React v16. If you have ideas on how to add support for React v15, submit an issue and let's discuss!

Note that React will log an error to the console regarding the thrown error, but this can safely be ignored. Unfortunately, there is no way to disable this error reporting for these types of intentional use cases.

With that being said, I hope this module and accompanying demos make it easier to get up-to-speed with React Suspense. 😄

Install

npm install --save react-suspense-polyfill

Usage

The only difference between using this polyfill and a suspense-enabled version of React, is that you must import { Suspense } from react-suspense-polyfill instead of from React.

With this minor change, suspense demos and react-async-elements will function as expected.

import React, { Component } from 'react'
import ReactDOM from 'react-dom'

import { Suspense } from 'react-suspense-polyfill'

import { createCache, createResource } from 'simple-cache-provider'

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const cache = createCache()

// Loads the Thing component lazily
const getThing = createResource(
  () => sleep(2000).then(() => import('./Thing').then(mod => mod.default)),
  thing => thing
)

const LazyThing = props => {
  const Comp = getThing.read(cache, props)
  return <Comp {...props} />
}

class Example extends Component {
  render () {
    return (
      <React.Fragment>
        <h1>Suspense</h1>

        <Suspense delayMs={500} fallback={<div>🌀 'Loading....'</div>}>
          <LazyThing />
        </Suspense>
      </React.Fragment>
    )
  }
}

ReactDOM.render(<Example />, document.getElementById('root'))

In this example, the following rendering steps will occur:

  1. React will invoke Example's render method.
  2. Suspense will get rendered which will in turn attempt to render LazyThing.
  3. The LazyThing will try to load its resource from the cache but fail and throw a Promise.
  4. Suspense (actually Timeout under the hood) will catch this Promise in its error boundary componentDidCatch.
  5. Suspense starts waiting for that Promise to resolve and kicks off a 500ms timeout. Currently, the Suspense subtree is rendering nothing.
  6. After 500ms, Suspense will timeout and display its fallback loading content.
  7. After another 1500ms (2000ms total), the LazyThing resource resolves.
  8. Suspense realizes it's child has resolved and again attempts to re-render its child.
  9. The LazyThing component synchronously renders the previously cached Thing component.
  10. All is right with the world 😃

Related

  • blog post - Gives more background and a deeper explanation of how the code works.
  • react-suspense-starter - Alternative which bundles a pre-built version of Suspense-enabled React allowing you to experiment with React Suspense right meow.
  • react-async-elements - Suspense-friendly async React elements for common situations.
  • fresh-async-react - More Suspense stuff (code, demos, and discussions).

License

MIT © transitive-bullshit

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].