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

priem

v2.0.0-alpha.12

Published

React Hook to declaratively subscribe to external data resources.

Downloads

14

Readme

priem · npm Build Status codecov bundlephobia

React Hook to declaratively subscribe to external data resources.

Table of Contents

Installation

yarn add priem

Getting Started

App.js

import React from 'react';
import {createResource} from 'priem';

const useReddit = createResource(
    () =>
        fetch('https://www.reddit.com/r/reactjs.json')
            .then(res => res.json())
            .then(res => res.data.children),
    {
        ssrKey: 'reddit-resource',
    },
);

export default () => {
    const [redditData, {pending}] = useReddit({});

    if (!redditData) {
        return pending ? <h2>Loading...</h2> : <h2>Empty.</h2>;
    }

    return (
        <div style={{opacity: pending ? 0.5 : 1}}>
            <ul>
                {redditData.map((post, i) => (
                    <li key={i}>{post.title}</li>
                ))}
            </ul>
        </div>
    );
};

server.js

import React from 'react';
import ReactDOM from 'react-dom/server';
import {getDataFromTree} from 'priem/server';
import {flushStore} from 'priem';
import App from './App';

app.get(async (req, res) => {
    await getDataFromTree(<App />);
    const content = ReactDOM.renderToString(<App />);

    // We suggest to use a specific library instead of JSON.stringify
    // for example `devalue` or `serialize-javascript`.
    const storeJson = JSON.stringify(flushStore()).replace(/</g, '\\u003c');

    res.send(`
        <!doctype html>
        ${ReactDOM.renderToStaticMarkup(<Html content={content} />)}
        <script id="preloaded-state">
            window.__PRIEM_STORE__ = ${storeJson};
        </script>
    `);
});

client.js

import React from 'react';
import ReactDOM from 'react-dom';
import {hydrateStore} from 'priem';

hydrateStore(JSON.parse(window.__PRIEM_STORE__));
delete window.__PRIEM_STORE__;

// Note that the import order is important here
const App = require('./App').default;

ReactDOM.hydrate(<App />, document.getElementById('root'));

Examples

Example apps can be found under the examples/ directory.

API

createResource

Creates a React Hook that fetches and caches data.

Arguments

  1. fn: (AsyncFunction): An async function that takes arguments from useResource and must return a Promise. If promise rejects, the cache item corresponding to these arguments will have a rejected status.
  2. options (Object): An options object, that can have the following properties:
    • maxAge (number?): A time in milliseconds after which cache items will expire and trigger a refresh.
    • maxSize (number?): A number of maximum cache entries in store. After exceeding this amount the most former used item will be removed and a refresh triggered. Defaults to 1.
    • refreshOnMount (Boolean?): Refreshes data on mounting. Default to false.
    • ssrKey (string?): A unique key that will be used to place this resource to the store. Required for server-side rendering.

Returns

useResource.

useResource

A React Hook for subscribing to resources.

Arguments

  1. args (Record<string, unknown> | undefined): An array of arguments that will be passed to a function in createResource. Can also be undefined which will prevent the update which can be utilized for waiting for other async tasks or user interactions to finish. Defaults to [].

Returns

The function returns a tuple with data and a meta object:

  1. data (any): The last successful data. Defaults to undefined.
  2. meta (Object): Meta properties of most recent promise.
    • pending (boolean).
    • rejected (boolean).
    • reason (Error?).
    • refresh (Function): a method to update the resource.

getDataFromTree(element)

An async function that walks the component tree and fetches data from resources that have ssrKey set. Returns a promise that either resolves with undefined or rejects on errors.


hydrateStore(initialStore)

A function to hydrate internal store with initial data from server.


flushStore()

A function that clears internal store and returns it. It's safe to serialize it and send to client.