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-list-provider

v1.1.1

Published

Use Context to save lists of whatever you want, then access them anywhere in your componpent tree.

Downloads

21

Readme

React List Provider

Use Context to save lists of whatever you want, then access them anywhere in your componpent tree.

index.js

import { ListProvider } from 'react-list-provider';
import App from './app';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <ListProvider name="notifications">
    <App />
  </ListProvider>,
  document.body
);

app.js

const { useList } = require('react-list-provider');

export const App = () => {
  const { addItem, items } = useList({ name: 'notifications' });

  return (
    <div>
      {items.map((item) => (
        <div>Notification: {item.message}</div>
      ))}
      Example application
      <button
        onClick={() => addItem({ id: Date.now().toString(), message: 'Hi' })}
      >
        Say hi
      </button>
    </div>
  );
};

Installation

yarn add react-list-provider

or, for npm:

npm install --save react-list-provider

API

import { ListProvider, ListConsumer, useList, withListManager } from 'react-list-provider';

<ListProvider>

A React Context Provider for an instance of a list of items.

<ListProvider name="notifications" keyBy="id">
  <App />
</ListProvider>

Props

| Prop | isRequired | Description | | ------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | name | ✅ | A unique name used to identify the list. Must match a corresponding usage of useList/withListManager/ListConsumer | | keyBy | | Enforce uniqueness by setting a key to use when adding items. For example, setting 'id' will ensure all added items have a unique 'id' field. If omitted, no uniqueness will be enforced, and only items/addItems/clearItems methods are exposed on the context. |

List Methods

Each of useList/withListManager/ListConsumer return a context object which contains the following keys:

| | Type | Description | | ---------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | | items | Array<Item> | The list items as added with addItem | | addItem | Function(<Item>): void | Add an individual item to the list. If the <ListProvider> has keyBy set, the item passed to addItem must contain that field. | | clearItems | Function(): void | Remove all items from the list. | | updateItem* | Function(key, <Item>): void | Update a single item in the list. Performs a shallow merge. | | removeItem * | Function(key): void | Remove a single item from the list. | | removeItems * | Function(key): void | Remove multiple items from the list. | | hasItem * | Function(key): Boolean | Check if an item in the list has the given key. | | getItem * | Function(): <Item> | Retrieve the item in the list with the given key (or undefined). |

* Only available when keyBy is set on the <ListProvider>

<ListConsumer>

A React Context Consumer, used to access the list methods.

<ListConsumer name="notifications">
  {(context) => {
    // context.items, context.addItem, ...etc
    return <App />;
  }}
</ListConsumer>

Props

| Prop | isRequired | Description | | ------ | ---------- | --------------------------------------------------------------------------------------------- | | name | ✅ | A unique name used to identify the list. Must match a corresponding usage of <ListProvider> |

useList(config)

A React Hook, used to access the list methods.

const Component = () => {
  const { items, addItem, ...etc } = useList({ name: 'notifications' });
  return <App />;
};

config

| | isRequired | Description | | ------ | ---------- | --------------------------------------------------------------------------------------------- | | name | ✅ | A unique name used to identify the list. Must match a corresponding usage of <ListProvider> |

withListManager(Component, config)

Creates a React Higher Order Component which injects a prop named listManager; an object containing the list methods.

const Component = ({ listManager }) => {
  // listManager.items, listManager.addItem, etc...
  return <App />;
};

const ComponentWithList = withListManager(Component, { name: 'notifications' });

Component

The existing React component to inject the prop listManager into.

config

| | isRequired | Description | | ------ | ---------- | --------------------------------------------------------------------------------------------- | | name | ✅ | A unique name used to identify the list. Must match a corresponding usage of <ListProvider> |

Nested Providers

It's possible to nest <ListProvider>s, enabling multiple lists to co-exists.

Nested Providers must be given a unique name to differentiate them:

import { ListProvider, ListConsumer } from 'react-list-provider';
const App = () => (
  <ListProvider name="accounts">
    <ListProvider name="notifications">
      <ListConsumer name="notifications">
        {({ addItem }) => (
          <button onClick={() => addItem('Hello')}>Add Notification</button>
        )}
      </ListConsumer>
      <ListConsumer name="accounts">
        {({ addItem }) => (
          <button onClick={() => addItem('Jess')}>Add Account</button>
        )}
      </ListConsumer>
    </ListProvider>
  </ListProvider>
);