My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Redux API Saga - React API calls made simple

Arun Kumar T K's photo
Arun Kumar T K
·Feb 7, 2019

Takes in a config and gives you a ready to use reducer saga and a common action. Avoid writing unnecessary boilerplate code. Github -> redux-api-saga

Example code which makes an API call to localhost:3001/puppyJpg/20345?resolution=HD

config --->

[{
    path: 'http://localhost:3001/puppyJpg/:imageId',
    method: 'GET',
    name: 'puppyJpg',
    mode: 'takeLatest',
    initialResult: '',
}]

Usage --->

this.props.dispatch(action({
  name: 'puppyJpg',
  payload: {},
  params: { imageId: 20345 },
  query: { resolution: 'HD' },
  onSuccess: () => {},
  onError: (error) => {
    console.error(typeof error === 'string' ? error : error.message);
  };
}));