API-Calls-store
API Calls store for react redux
How to use
Install
npm install --save api-calls-store
Configure
Add the reducer
In the reducers.js
file:
import APIReducer from "api-calls-store/src/store/api/reducer";
export default combineReducers({
APIReducer,
});
Add the saga
In the sagas.js
file:
import { apiSaga, initializeAPIStore } from "api-calls-store/src/package-index";
const clockCount = 15;
initializeAPIStore({
getAPI,
clockCount,
});
export default function* rootSaga() {
yield all([
fork(apiSaga),
]);
}
Add the dispatch clock
In the App.js
add this useEffect
to dispatch the clock every Xms.
This clock will dispatch an action in the saga every Xms and that action will call the API.
useEffect(() => {
const interval = setInterval(() => {
dispatch({ type: "API_DISPATCH_CLOCK" });
}, 1000);
return () => clearInterval(interval);
}, [dispatch]);
Actions
export const API_CALL = "API_CALL";
export const API_CALL_SUCCESS = "API_CALL_SUCCESS";
export const API_CALL_FAIL = "API_CALL_FAIL";
export const API_ADD_SUBSCRIPTION = "API_ADD_SUBSCRIPTION";
export const API_REMOVE_SUBSCRIPTION = "API_REMOVE_SUBSCRIPTION";
export const API_DISPATCH_CLOCK = "API_DISPATCH_CLOCK";
export const API_SUBSCRIPTION_INCREASE_CLOCK = "API_SUBSCRIPTION_INCREASE_CLOCK";
export const API_INCREASE_CLOCK = "API_INCREASE_CLOCK";