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 🙏

© 2022 – Pkg Stats / Ryan Hefner

react-request-block

v0.2.3

Published

React component to compose requests within your components. Supports server-side rendering requests.

Downloads

47

Readme

react-request-block

React component to compose requests within your components, with support for server-side rendered requests.

Install

Via npm:

npm install -S react-request-block

Via Yarn:

yarn add react-request-block

How to use

react-request-block was built with a focus on simplicity and flexibility, and with out-of-the-box support for use within server-side rendering frameworks (more on that below).

RequestBlockProvider

The basic wrapper that provides caching for the nested RequestBlock instances and the ability to provide a default origin to prepend onto the RequestBlock urls.

Properties

| Property | Default | Description | | ---------------- | ----------------------- | ----------- | | cache | new RequestBlockCache | RequestBlockCache instance for caching request made per provider. By default, a fresh cache instance is created if one is not supplied. | | options | null | Default options to be included with each request made by a RequestBlock. Can be ignored by adding ignoreContextOptions to RequestBlock instances. | | origin | '' | Default origin to prepend to RequestBlock requests. Defaults to the current site’s protocol/host. | | renderPromises | null | This is used strictly for server-side rendering instances. In most cases you shouldn’t worry about setting this, unless you need to. In which case, it accepts a RenderPromises instance. |

RequestBlock

The RequestBlock is what you will be interacting with most. It makes it really easy to compose requests into your pages and components and handles all the caching for you behind the scenes. A simple alternative to achieving a similar thing with redux/react-redux.

Properties

| Property | Default | Description | | ---------------------- | ------------------------------------------- | ------------ | | url | null | URL to make request to. If origin specified on RequestBlockProvider, url will be appended to it. | | options | null | Options to include with fetch request made. All normal fetch options accepted. | | parser | (data, props) => data | Method for manipulating the response payload before it is applied to the data value. Handy for stripping out unwanted stuff, or making it more useable for your page or component. | | skip | false | Whether or not the request should be skipped during server-side rendering. | | ignoreContextOptions | false | Flag to ignore options set on the RequestBlockProvider for requests being made by RequestBlock. | | onRequest | ({ data, error, fetched, loading }) => {} | Callback made when a request is initiated. | | onLoad | ({ data, error, fetched, loading }) => {} | Callback made when request has successfull completed. | | onError | ({ data, error, fetched, loading }) => {} | Callback made when a request encounters an error. |

withRequestBlock (HOC)

Provides access to the current context of the ReactRequestBlock. Context passed to the wrapped component includes:

Properties

| Property | Type | Description | | ---------------- | ------------------- | ----------- | | cache | RequestBlockCache | The current RequestBlockCache instance being used by the provider. | | options | object | null | Default request options that have been set on the RequestBlockProvider. | | origin | string | Origin to prepend to urls being requested by RequestBlock instances. | | renderPromises | RenderPromises | RenderPromises instance set for use during server-side rendering. |

Examples

Below are a few examples of using the RequestBlockProvider and two ways you can use the RequestBlock.

RequestBlockProvider

import React from 'react';
import { RequestBlockCache, RequestBlockProvider } from 'react-request-block';
import Page from './Page';  // @see Page component defined in `RequestBlock` example below

const App = () => (
  <RequestBlockProvider>
    <Router>
      <Switch>
        <Route path="/:slug*" component={Page} />
      </Switch>
    </Router>
  </RequestBlockProvider>
);

export default App;

RequestBlock (wrapping content)

In this example, the RequestBlock components allows you to structure requests right into your pages or components.

import React from 'react';
import { RequestBlock } from 'react-request-block';

const Page = (props) => (
  <RequestBlock url="[your endpoint here]">
    {({data, error, fetched, loading}) => {
      if (loading || !fetched) {
        return null;
      }

      if (error) {
        console.error(error);
        return null;
      }

      if (!data) {
        return <p>Page does not exist.</p>;
      }

      // See the Contentful query response
      console.debug(data);

      // Process and pass in the loaded `data` necessary for your page or child components.
      return (
        ...
      );
    }}
  </RequestBlock>
);

export default Page;

RequestBlock (w/ callbacks)

import React, { Component } from 'react';
import { RequestBlock } from 'react-request-block';

class Page extends Component {
  constructor(props) {
    super(props);

    this.state = {
      loading: false,
      data: null,
      error: null,
    };

    this.onRequest = this.onRequest.bind(this);
    this.onLoad = this.onLoad.bind(this);
    this.onError = this.onError.bind(this);
  }

  onRequest() {
    this.setState({
      loading: true,
    });
  }

  onLoad({ data }) {
    this.setState({
      loading: false,
      data,
    });
  }

  onError({ error }) {
    this.setState({
      loading: false,
      error,
    });
  }

  render() {
    return (
      <React.Fragment>
        <RequestBlock
          url="[your endpoint here]"
          onRequest={this.onRequest}
          onLoad={this.onLoad}
          onError={this.onError}
        />
        {loading && <p>Loading...</p>}
        {error && <p>{error}</p>}
        {data && [something that uses data]}
      </React.Fragment>
    );
  }
}

export default Page;

Using Next.js?

If you like what you see above, you might like next-request-block, which lets you easily add react-request-block to your Next.js app. Making it easy to ensure that all your RequestBlock instances render awesomely server-side.

License

MIT © Ryan Hefner