1.0.0 • Published 5 years ago
flux-framework v1.0.0
flux-framework
My first flux-based framework
Installation
npm i -D ajmdag/flux-framework
Development
git clone https://github.com/Ajmdag/flux-framework.git
cd flux-framework
npm i
Documentation
Usage
To use this library, you should create Store instance.
import Store from 'flux-framework';
const store = new Store(stateUpdator, initialState);
stateUpdator
and initialState
are required.
stateUpdator
stateUpdator
is a function that knows how to change state, depending on the input data. Should return new state.
function stateUpdator(state, action) {
switch(action.type) {
case 'INCREMENT':
return { count: state.count + action.amount };
case 'DECREMENT':
return { count: state.count - action.amount };
default: return state;
}
}
initialState
initialState
is a data, that describes the initial state of your application or component.
const initialState = {
count: 0
};
create actions
const incrementAction = { type: "INCREMENT", amount: 3 };
const decrementAction = { type: "DECREMENT", amount: 5 };
udpate store with actions
store.update(incrementAction);
store.update(decrementAction);
subscribe
subscribe(callback)
. callback
is a function that will be called when store will be changed.
store.subscribe(() => console.log("State changed", store.state));
unsubscribe
To unsubscribe from store changes just put store.subscribe(callback)
in a variable(or constant) and call it as a function.
const unsubscribe = store.subscribe(() => console.log("State changed", store.state));
unsubscribe();
getState
To get a store use this method:
store.getState;