Alkstore is a simple, fast and awesome state container for your JavaScript apps.
It is mainly inspired by redux but it's lighter and simpler.
npm install alkstore
or
yarn add alkstore
or
pnpm add alkstore
- Define initial state
// initial-state.ts
export const initialState = {
count: 0,
name: 'John',
isOnline: false,
};
export type RootState = typeof initialState;
- Define actions
// actions.ts
export function incrementCount(incrementValue = 1) {
return { type: 'increment', payload: { count: incrementValue } };
}
export function decrementCount(decrementValue = 1) {
return { type: 'decrement', payload: { count: decrementValue } };
}
export function resetCount(reset = 0) {
return { type: 'reset', payload: { count: reset } };
}
- Define reducer
// reducers.ts
import { combineReducers } from 'alkstore';
import type { Action, Reducers } from 'alkstore';
import { RootState } from './initial-state';
const countReducer: Reducers<RootState, Action<RootState>> = {
increment(state, action) {
return {
...state,
count: action.payload.count ? state.count + action.payload.count : state.count,
};
},
decrement(state, action) {
return {
...state,
count: action.payload.count ? state.count - action.payload.count : state.count,
};
},
reset(state, action) {
return {
...state,
count: action.payload.count ?? 0,
};
},
};
const reducers = combineReducers<RootState>(countReducer);
- Create store and provide app with it
// index.ts
import React from 'react';
import ReactDOM from 'react-dom/client';
import { AlkstoreProvider, createStore } from 'alkstore';
import { initialState, reducers, RootState } from './store';
import App from './App';
const store = createStore<RootState>({
initialState,
reducers,
});
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<AlkstoreProvider<RootState> store={store}>
<App />
</AlkstoreProvider>
</React.StrictMode>,
);
- Use store in your app
// app.tsx
import { useSelector } from 'alkstore';
import { useAppDispatch, selectCount, decrementCount, incrementCount, resetCount } from './store';
function App() {
const dispatch = useAppDispatch();
const count = useSelector(selectCount);
return (
<div>
<p>
You have clicked the button
<strong> {count}</strong> times.
</p>
<div>
<button onClick={() => dispatch(incrementCount())}>Increment</button>
<button onClick={() => dispatch(decrementCount())}>Decrement</button>
<button onClick={() => dispatch(resetCount())}>Reset</button>
</div>
</div>
);
}
export default App;
- In order to run the application, you need to have installed pnpm.
npm install -g pnpm
- Clone repository and install dependencies
git clone alkstore
cd alkstore
pnpm install
- Go to
example
folder and install dependencies
cd example
pnpm install
- Run example
pnpm dev
pnpm build
- build alkstore packagebuild:esm
- build esm modulesbuild:cjs
- build cjs modulespnpm lint
- run linterpnpm test
- run tests
pnpm dev
- run example app in development modepnpm build
- build example apppnpm preview
- preview production build