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;
app.js
const useList = ; const App = const addItem items = ; return <div> items Example application <button = > Say hi </button> </div> ;;
Installation
yarn add react-list-provider
or, for npm
:
npm install --save react-list-provider
API
;
<ListProvider>
A React Context Provider for an instance of a list of items.
<ListProvider ="notifications" ="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 ="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 = ; 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 = ;
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 ="accounts"> <ListProvider ="notifications"> <ListConsumer ="notifications"> addItem <button =>Add Notification</button> </ListConsumer> <ListConsumer ="accounts"> addItem <button =>Add Account</button> </ListConsumer> </ListProvider> </ListProvider>;