expose-state
A helper React HOC to return a component that will re-render when a value is changed.
Installation
# npm npm install expose-state # yarn yarn add expose-state
Example
import exposeState from 'expose-state'import React from 'react'import render from 'react-dom' const GlobalError globalErrorState = null const Message = text <div>text</div> window
API
exposeState(render): initialState => [WrapperComponent, Store]
A helper React HOC to return a component that will re-render when a value is changed.
Argument | Type | Description |
---|---|---|
render |
(state, props) => Node |
Passed the current state value and the props passed to the wrapped component. Returns a React node. |
initialState |
* |
Initial state value. |
Returns a tuple. The first item is the wrapped component subscribed to the store, and the second item is the store.
Property | Type | Description |
---|---|---|
store.getState |
(): * |
Returns the current state value. |
store.setState |
(nextState): void |
Sets the state to the given value and executes listeners. |
store.subscribe |
(state => *): () => void |
Subscribe the given listener function to updates; it will be invoked with the next state. Returns a function that can be called to unsubscribe. |
const ShowCount store = 0 const unsubscribe = store const currentCount = storestore