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

v1.0.0-rc.2

Published

useRequest hook & set of components for requesting API data within React applications

Downloads

5

Readme

react-sadness

CI Workflow pre-commit code style: prettier npm version npm license

useRequest hook & set of components for requesting API data within React applications.

  • Built on top of axios
  • Supports prerendering
  • Provides common components for API needs

Requirements

Installation

npm install axios react react-dom react-sadness

IMPORTANT: You need to install axios, react & react-dom in your project additionally, as they listed as peer dependencies to react-sadness. Installing just react-sadness will not be enough.

Quickstart

First, you need to wrap your app container into the SadnessProvider & mount it into the DOM element (instead of render or hydrate),

import React from "react";
import { mount, SadnessProvider } from "react-sadness";
import { BrowserRouter } from "react-router";

mount(
  <SadnessProvider>
    <AppContainer />
  </SadnessProvider>,
  document.getElementById("ui")
);

Next, to request data from the API anywhere inside of AppContainer,

import I from "immutable";
import React from "react";
import { Response, useRequest } from "react-sadness";

import { toUser } from "../records/User";

const toList = (data) => new I.List(data.map(toUser));

const Users = () => {
  const { state } = useRequest("/users", { responseDataConverter: toList });
  return (
    <Response state={state}>
      {(users) => (
        <ul>
          {users.map((item) => (
            <li key={item.id}>{item.username}</li>
          ))}
        </ul>
      )}
    </Response>
  );
};

Prerendering

IMPORTANT: Example below illustrates prerendering data with parcel-plugin-prerender plugin.

react-sadness supports prerendering by triggering readyEvent via SadnessReady HoC.

import { SadnessReady } from "react-sadness";

const App = () => (
  {/* Ready event will trigger after both child requests will done */}
  <SadnessReady>
    {/* Request projects from API */}
    <Projects />
    {/* Request talks from API */}
    <Talks />
  </SadnessReady>
)

Afterwards, you need to setup parcel-plugin-prerender to wait before readyEvent, such as,

  "prerender": {
    "rendererConfig": {
      "renderAfterDocumentEvent": "react-sadness-ready"
    }
  }

In case if children nodes does not contain any planned API requests, pass force prop to SadnessReady component to force triggering ready event,

const About = () => (
  <SadnessReady force>
    <AboutContent />
  </SadnessReady>
);

Examples

Visit react-sadness.vercel.app to browse through react-sadness Storybook.

Or run,

make run

to start Storybook server at http://localhost:6006.