Comment on page
List
Simple list that can be hooked into React.
yarn
npm
yarn add @corets/list
npm install --save @corets/list
Seamless React integration is shipped in a separate package:
There is a version of this package that syncs directly to the
localStorage
:Creates a new instance of observable list:
import { createList } from "@corets/list"
const list = createList(["some", "data"])
Create a new instance without the factory function:
import { List } from "@corets/list"
const list = new List(["some", "data"])
Create a new instance with a custom differ:
import { createList } from "@corets/list"
const differ = (oldValue, newValue) => true
const list = createList(["some", "data"], { differ })
Retrieve everything from the list:
list.get()
Retrieve a value at the specific index:
list.getAt(0)
Replace everything in the list:
list.set(["mango", "potato"])
Append some new values to the list:
list.add("oranges", "beans")
Add a value at the specific index:
list.addAt(1, "chili")
Check if a specific value is in the list:
list.has("oranges")
Check if there is a value at the specific index:
list.hasAt(1)
Remove a value from the list:
list.remove("beans")
Remove value at the specific index:
list.removeAt(1)
Get index of a specific value:
list.indexOf("oranges")
Filter values in the list:
list.filter((value, index) => true)
Map values in the list:
list.map((value, index) => newValue)
Iterate over values in the list:
list.forEach((value, index) => {})
Listen to changes:
list.listen(state => console.log(state))
Invoke listener immediately:
list.listen(state => console.log(state), { immediate: true })
Listen with a custom differ:
const differ = (oldValue, newValue) => true
list.listen(state => console.log(state), { differ })
Convenience method for people used to React's
useState
syntax:import { createList } from "@corets/list"
const [list, setList] = createList([1, 2]).use()
Last modified 2yr ago