A simplified redux store for react
npm install --save simple-react-store
Simple React Store is a simplified version of Redux
Returns a new Store with the initial state set. If the initial state is not provided defaults to an empty object
initialState (any)[{}]
: The initial state you want to set. Defaults to empty object.
(Store instance)
: A new Store with the initial state set
import { Store } from 'simple-react-store'
// Create store with empty state
const aStore = new Store()
// Create store with initial state
const anotherStore = new Store({
name: 'John Doe',
user: 'john.dow'
})
Returns the saved state
(any)
: The previously saved state. Could be any type of value.
import { Store } from 'simple-react-store'
const aStore = new Store({
name: 'John Doe',
user: 'john.dow'
})
console.log(aStore.getState())
// {
// name: 'John Doe',
// user: 'john.dow'
// }
Saves the new state into the store. Remember keep your states immutable.
someState (any)
: The state you want to save.
actionName (string)['']
: Optionally you can identified the state change with an action name. The name will be shared with all the state change subscribers and redux dev tools.
import { Store } from 'simple-react-store'
const aStore = new Store()
const newState = {
name: 'John Doe',
user: 'john.dow',
nationality: 'argentinian'
}
aStore.setState(newState, 'Set user nationality')
console.log(aStore.getState())
// {
// name: 'John Doe',
// user: 'john.dow'
// nationality: 'argentinian'
// }
Produce and save a new immutable state based on the current state.
stateProducer (function)
: A function that will received the next state as a parameter and can be mutated. Simple react store will use immer to generate the immutable state.
actionName (string)['']
: Optionally you can identified the state change with an action name. The name will be shared with all the state change subscribers and redux dev tools.
import { Store } from 'simple-react-store'
const aStore = new Store({
name: 'John Doe',
user: 'john.dow'
})
aStore.updatedState((nextState) => {
nextState.nationality = 'argentinian'
})
console.log(aStore.getState())
// {
// name: 'John Doe',
// user: 'john.dow'
// nationality: 'argentinian'
// }
Subscribes for state changes
callback (function)
: The function that will be executed any time there is a new state.
state (any)
: The saved state.
actionName (string)
: Action name set when updated the state
import { Store } from 'simple-react-store'
const aStore = new Store()
const newState = {
name: 'John Doe',
user: 'john.dow'
}
aStore.onUpdate((state, actionName) => {
// do something
})
aStore.setState(newState, 'Set user session details')
Updates the props of a react component every time there is a new state
resolvePropsCallback (function(state, [ownProps]))
: The function that will be executed any time there is a new state. It has to return an object of props.
Component (React Component)
: The react component that will receive the props resolved by resolvePropsCallback
(React Component)
: A hight order component that will update the given component any time there are new props resolved by resolvePropsCallback
import { Store } from 'simple-react-store'
import App from './components/App.js'
const aStore = new Store({
name: 'John Doe',
user: 'john.dow'
})
const resolveProps = (state, ownProps) => {
return {
title: 'I love simple-react-store',
user: state.user
}
}
const Container = aStore.connect(resolveProps)(App)
// <App
// title='I love simple-react-store'
// user='John Doe'
// />
Connect a store to redux dev tools if available. Any state saved into the store will be logged as long with the given action name
aStore (Store instance)
: The created Store instance
import { Store, connectDevTools } from 'simple-react-store'
const aStore = new Store()
connectDevTools(aStore)