allstore
Version:
A React Single-Truth store in 94% less code-lines by Harald Rudell
101 lines (72 loc) • 3.81 kB
Markdown
# AllStore
## © 2018-present Harald Rudell <harald.rudell@gmail.com> (http://www.haraldrudell.com)
 
**Single-truth store in 94% less code-lines**
Demonstration Project: Demo-Context-Store<br />
 [Live](https://haraldrudell.github.io/demo-context-store/)<br />
 [source code](https://github.com/haraldrudell/demo-context-store)
[Allstore source code](https://github.com/haraldrudell/demo-context-store/tree/master/src/allstore)
[Allstore npm package link](https://www.npmjs.com/package/allstore)
### Features
* Single-truth store as plain object
* useAllstore React 16.7 hook or traditional connect high-order component
* Defaults to pure components, support for traditional components
### Design
* Store manipulation via ECMAScript module imports
* No name-collision via import aliasing and configurable store-slice names
* Use of React 16.3 context and internal subscription mechanic
 
Published using **[Lib Create React App](https://www.npmjs.com/package/lib-create-react-app)** that allows React components to be publicly published as npm packages
[click for **Video presentation**, 10 min](https://youtu.be/KVaOVjiH2SQ)
 
### Usage
* **yarn add allstore**
<blockQuote>
import { Store } from 'allstore'<br/>
export default () =>
<Store store={{value: 1}}>
…
</Store></blockQuote>
 
<blockQuote>
import { store, Store } from 'allstore'<br/>
Object.assign(store, {value: 1})<br/>
export default () =>
<Store>
…
</Store></blockQuote>
 
<blockQuote>
import { OrderedMap, Map } from 'immutable'<br/>
import { store, useAllstore } from 'allstore'<br/>
let sliceName1 = 'records' // modifiable to avoid collisions setSlicename(s)<br/>
const setRecords = list => store[sliceName1] = OrderedMap(list.map(o => [o.id, Map(o)]))<br/>
const recordsSelector = (state, {id}) => ({record: state[sliceName1].get(id)})<br/>
const getRecord = r => (r && r.toJS()) || {}<br/>
setRecords([{id: 1, record: 'One'}, {id: 2, record: 'Two'}])<br/>
function DisplayRecord({id}) {<br/>
 const {record: map} = useAllstore(recordsSelector, {id})<br/>
 const {record} = getRecord(map)<br/>
 return <div>id: {id} record: {record || ''}</div><br/>
}</blockQuote>
### API Reference
import { store, Store, useAllstore, connect, notify, getState, subscribe, storeContext, StoreProvider, StoreConsumer } from 'allstore'
* **Store**: component that makes the store context available, see Usage
* const {storeProps…} = **useAllstore(mapStateToProps, {props…}, [pure: false])**: React 16.7 hook, see Usage
props to Component
* **connect(mapStateToProps, options)(Component)**: provides store properties as
* **mapStateToProps(Object state, Object props) Object**: defines what props the wrapped component needs based on the store state and its other props
* **options: {displayName: string, pure: boolean: default true}**
* **store**: plain store object
* **notify()** all components evaluate the new store state for redraw
* **subscribe()** subscription for store changes, typically only used by connect or useAllstore
* **getState()** gets the store plain object to allow for modifications
 
* **storeContext** for a class that wants to access the store context, used by connect
* **StoreProvider StoreConsumer** for custom rendering of the React 16.3 context
 
## link: [Hire Harald Rudell](https://hire.surge.sh/)
## link: [Sponsor Harald Rudell](https://www.gofundme.com/san-francisco-revenge-crime-victim/)
 
**[Lib Create React App](https://www.npmjs.com/package/lib-create-react-app)** creates shareable libraries from projects bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## © 2018-present Harald Rudell <harald.rudell@gmail.com> (http://www.haraldrudell.com)