StoreComponent

StoreComponent class inherits from Component

StoreComponent.setStateSetter(stateSetter, async)

Arguments

Arguement Type Description
stateSetter Function a function to set the state
async boolean is the state setter async

if async is true, the stateSetter has the following signature

function stateSetter(state, done)

otherwise

function stateSetter(state)

Set the state setter for a todo list app with localStorage

store.setStateSetter(function(state){
  localStorage.setItem('todos', JSON.stringify(state));
});

StoreComponent.setStateGetter(stateGetter, async)

Arguments

Arguement Type Description
stateGetter Function a function to get the state
async boolean is the state getter async

if async is true, the stateGetter has the following signature

function stateGetter(state, done)

done(error, newState)

otherwise

function stateGetter(state): newState

Set the state getter for a todo list app with localStorage

store.setStateGetter(function(){
  let todos = localStorage.getItem('todos');
  if (todos) return JSON.parse(todos);
  else return null;
});

StoreComponent.setStateInitiator(stateInitiator, async)

Arguments

Arguement Type Description
stateInitiator Function a function to init the state
async boolean is the state initiator async

if async is true, the stateInitiator has the following signature

function stateInitiator(done)

done(error, initState)

otherwise

function stateInitiator(): initState

Set the state initiator for a todo list app

store.setStateInitiator(function(){
  return {
    todos: []
  }
});

StoreComponent.setInitStateSaver(initStateSaver, async)

Arguments

Arguement Type Description
initStateSaver Function a function to save the initial state
async boolean is the initial state saver async

if async is true, the stateInitiator has the following signature

function initStateSaver(state, done)

done(error, newState)

otherwise

function initStateSaver(state): newState

Set the state initiator for a todo list app

store.setInitStateSaver(function(state){
  let todos = localStorage.getItem('todos');
  if (!todos) {
    localStorage.setItem('todos', JSON.stringify(state));
    return state;
  } else {
    return todos;
  }
});

StoreComponent.reduce(actionName, reducer)

Create a reducer to handle action

Arguments

Arguement Type Description
action String the action to handle
reducer Function function(prevState, action) : void

Create a reducer to handle 'INCREMENT' action

store.reduce('INCREMENT', (prevState, action) => {
  return prevState + 1;
})

 

 

 

results matching ""

    No results matching ""