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-xhr

v0.0.7

Published

React-flavored universal XHR

Downloads

14

Readme

react-xhr

The problem

  • Data fetching for universal JS apps is hard. If you do any XHRs their data isn't present in the server-rendered markup.
  • Often you will want to send down the initial state of the app along with the webpage that bootstraps it.

What this does not solve

  • The Relay "n+1 queries problem". It's possible for you to stumble into really inefficient data fetching patterns with this. You should build some sort of batching abstraction on top of this.
  • Client-side mutations (use Flux + this)

How to use it

Declaratively specify your XHRs:

var React = require('react');
var ReactXhr = require('react-xhr');

var MyComponent = React.createClass({
  mixins: [ReactXhr.Mixin],

  getXhrs: function() {
    return {
      userInfo: {
        url: '/user/' + this.props.username,
        method: 'get',
      },
    };
  },

  render: function() {
    return <div>User info: {this.state.xhrs.userInfo.body}</div>;
  },
});

this.state.xhrs.X has the following properties:

  • loading: true or false if there's a pending request
  • err: any error object associated with the most recent request
  • body: parsed JSON response returned from the server, empty object if no response yet.

This in and of itself is pretty great, but you can render it server-side if you provide your own function to resolve the fetching:

// On the server

function mockXhrs(xhrs, cb) {
  // Mock backend call
  var response = {};
  for (var xhrId in xhrs) {
    var xhr = xhrs[xhrId];
    if (xhr.url.indexOf('/user/') === 0) {
      response[xhrId] = {username: xhr.url.slice('/user/'.length), isCool: true};
    }
  }
  cb(response);
}

ReactXhr.renderToString(
  <MyComponent username="floydophone" />,
  mockXhrs,
  function(err, result) {
    console.log(result.markup);
    console.log(result.preloadedData);
  }
);

Now that you have the markup and data that you need to preload on the client, you can render on the client easily:

// On the client

ReactXhr.render(<MyComponent username="floydophone" />, preloadedData);

Advanced features

You can do other types of requests, not just xhrs. getXhrs() just returns a description of what to fetch. You can plug in your own if you want to do fetching via websocket, batched fetching, etc.

// On the client
ReactXhr.overridePerformXhr(function(xhr, cb) {
  // Read properties off of xhr (as returned from getXhrs()) and call cb(err, body)
  // with the results
});