npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-ideal

v0.0.4

Published

Run isomorphic React apps on the server without a DOM polyfill

Downloads

11

Readme

Overview

react-ideal allows you to run React apps on the server without a DOM polyfill. It allows the view tree to update over time, so you can wait until it's ready before rendering static markup.

The view tree exists as an abstract, ideal version of itself, updating over time as you'd expect in a client-side app. This allows you to implement true isomorphic rendering while maintaining the client-side model of data fetching, where each view loads its data independently.

In technical terms, this is an alternate renderer for React that provides crucial functionality omitted by react-dom/server.

Requires React version 16+.

TOC

Renderer Comparison

react-dom → browser, live/async

react-native → mobile, live/async

react-dom/server → Node, sync

react-ideal → Node, live/async → elements → react-dom/server → markup

Key differences with react-dom/server:

  • Creates a live component tree that updates over time
  • Properly handles unmounting
  • Doesn't render to string; you still need react-dom/server for that

Usage

Install from NPM:

npm install --exact react-ideal

Make sure you have React 16+:

npm install --exact react@16 react-dom@16

Use on server:

const {createElement} = require('react')
const {createContainer, renderToContainer, unmountAtContainer, containerToElements} = require('react-ideal')
const {renderToStaticMarkup} = require('react-dom/server')

function myRequestHandler(req, res, next) {
  const container = createContainer()
  const rootElem = createElement(MyReactComponent)
  renderToContainer(container, rootElem)

  somehowWaitUntilViewsAreReady(container, () => {
    const [element] = containerToElements(container)
    const markup = `<!doctype html>${renderToStaticMarkup(element)}`
    req.end(markup)
    unmountAtContainer(container)
  })
}

Gotchas

Async

Unlike react-dom/server, the initial rendering is asynchronous. If you're whipping up a small demo where rendering is done in one pass, make sure to wait for that pass to finish:

const container = createContainer()
const rootElem = createElement(MyReactComponent)
renderToContainer(container, rootElem, () => {
  const [element] = containerToElements(container)
  const markup = `<!doctype html>${renderToStaticMarkup(element)}`
  unmountAtContainer(container)
})

Readiness

To take advantage of react-ideal, you need the view components to signal their readiness status. For now, this is out of scope for react-ideal, but is trivial to implement. Here's a recipe:

const {createElement: E} = require('react')
const {createContainer, renderToContainer, unmountAtContainer, containerToElements} = require('react-ideal')
const {renderToStaticMarkup} = require('react-dom/server')
const PropTypes = require('prop-types')
const {Future} = require('posterus')

async function renderAsync() {
  const container = createContainer()
  const readiness = new Readiness()
  const rootElem = E(ReadinessContext, {readiness}, E(AsyncView))
  renderToContainer(container, rootElem)

  try {
    await readiness.future
    const [element] = containerToElements(container)
    const markup = `<!doctype html>${renderToStaticMarkup(element)}`
    return markup
  }
  finally {
    unmountAtContainer(container)
  }
}

class AsyncView extends PureComponent {
  constructor() {
    super(...arguments)
    this.state = {greeting: ''}
  }

  componentWillMount() {
    this.context.readiness.unready(this)

    setTimeout(() => {
      this.setState({greeting: 'Hello world!'})
      this.context.readiness.ready(this)
    }, 50)
  }

  render() {
    const {state: {greeting}} = this
    if (!greeting) return null
    return E('span', {className: 'row-center-center'}, greeting)
  }
}

AsyncView.contextTypes = {readiness: PropTypes.object}

class ReadinessContext extends PureComponent {
  getChildContext() {
    return {readiness: this.props.readiness}
  }
  render() {
    return this.props.children
  }
}

ReadinessContext.propTypes = {readiness: PropTypes.object}
ReadinessContext.childContextTypes = {readiness: PropTypes.object}

class Readiness {
  constructor() {
    this.future = new Future()
    this.notReady = new Set()
  }

  isFullyReady() {
    return !this.notReady.size
  }

  ready(component) {
    this.notReady.delete(component)
    if (this.isFullyReady()) this.future.arrive()
  }

  unready(component) {
    this.notReady.add(component)
  }
}