score:0

behavior such as ajax calls are referred to as "side effects", and generally live in either your components, "thunk" action creators, or other similar redux side effects addons such as "sagas".

please see this answer in the redux faq for more details:

score:1

the "teach a man to fish answer."

this depends on the type of call and the situation.

  1. generally for simple "gets" this can easily be done by placing them into your action creators as nader dabit has shown.
  2. there are many side effect management libraries which would opt for you to place them in their blocks(redux-sagas, axios calls, redux-thunk)

i use redux-sagas for now. at least until we decide yay or nay on async/await which is possibly coming in a newer version of js.

this may be the most important part!

just be sure to take into account the general "conventions" that are used with your particular set of tools usually found in the documentation, and be sure to google "best practices" in the future for things like this. this will help others new to your project to get their bearings and just jump in without ramping up learning your new customized version.

score:6

the easiest way to get started with this is to just add it into your actions, using a function called a thunk along with redux-thunk. all you need to do is add thunk to your store:

import { createstore, applymiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootreducer from './reducers/index';

const store = createstore(
  rootreducer,
  applymiddleware(thunk)
);

then create a function in your actions that calls the api:

export const getdata() {
  (dispatch) => {
    return fetch('/api/data')
      .then(response => response.json())
      .then(json => dispatch(resolvedgetdata(json)))
  }
}

export const resolvedgetdata(data) {
  return {
    type: 'resolved_get_data',
    data
  }
}

Related Query

More Query from same tag