Create store
Arguments
- Initial store state [object]
- WebWorker instance Returns Store object which is responble for the state of your app
Example
import { createStore } from "worker-store";
const initState = { count: 0, news: [] };
const store = createStore({}, new Worker());
store.dispatch("INCREMENT");
store.subscribe(message => console.log("new state", message.state));
Dispatches an action is the only way to trigger a state change.
Arguments
- Action name
- Payload
Example
connect()( ({dispatch}) => <button onClick={ dispatch('FETCH_NEWS' {id: 10})} )
Makes the store available to the connect() calls in the component hierarchy below.
Example
<Provider store={store}>
<MyComponent />
</Provider>
Connects a React/Preact component to the store.
Arguments
- mapStateToProps(state, [ownProps]): stateProps: If this argument is specified, the new component will subscribe to store updates. This means that any time the store is updated, mapStateToProps will be called. The results of mapStateToProps must be a plain object, which will be merged into the component’s props. If you don't want to subscribe to store updates, pass null or undefined in place of mapStateToProps.
const mapStateToProps = ( state, ownProps) => ({
count: state.count,
});
const App = connect(mapStateToProps)(({count}) => <h1>{count}/h1>);
Object with actions. Every action accepts current state and parameters passed from dispatch function
Can be used inside generator to update state
Parameters
- new state [object]