Search code examples
javascriptreduxredux-api-middleware

How does redux-api-middleware add data to the store?


Background

I am creating a universal react application with redux and react router. I have most of the application setup with server-side rendering and basic redux actions (modifying a Boolean in the store). Now I would like to make some api calls to get data for my app.

Current Implementation

I thought it would be a good idea to use the redux-api-middleware but I can't get the data to add to the store. I followed the documentation and created an action that looks like this.

example-action.js

import { CALL_API } from `redux-api-middleware`;


export function fn_get_data () {
  [CALL_API]: {
    endpoint: 'http://www.example.com/api/users',
    method: 'GET',
    types: ['REQUEST', 'SUCCESS', 'FAILURE']
  }
}

I run this action on a button click in my page. I see the action get fired but nothing goes into the store. I even added some custom code for the SUCCESS action and was able to console.log() the response but still could not get the data into the store.

Also I have added the middleware to the store in the same way the documentation says to.

configureStore.js

import { createStore, applyMiddleware, combineReducers } from 'redux';
import { apiMiddleware } from 'redux-api-middleware';
import reducers from './reducers';

const reducer = combineReducers(reducers);
const createStoreWithMiddleware = applyMiddleware(apiMiddleware)(createStore);

export default function configureStore(initialState) {
  return createStoreWithMiddleware(reducer, initialState);
}

Tried so far

So far I have tried a bunch of different things with the action, like making the actions into exportable symbols and calling them in a reducer and trying to merge the data into the current state that comes from the payload attribute in redux-api-middleware, but no luck.

Question

I really have 2 questions

  • Why are there no reducers in the documentation, is this just overlooked or does the data from the response just go directly into the store?
  • Why doesn't the data I call get added to the store (what am I missing)?

Any help, explanation or constructive criticism on the matter is really appreciated, thanks.


Solution

  • Solution

    Like markerikson said, the library only provides you with the tools, you still have to write reduceres to respond to the action. In my case I finally got data with the following reducer.

    example-reducer.js

    import * as ExampleActionType from "../action/example-action";
    import Immutable from "immutable";
    
    let defaultState = Immutable.fromJS({
      fakeData: {}
    });
    
    function exampleState (state = defaultState, action) {
      switch (action.type) {
    
        case ExampleActionType.REQUEST : {
            console.log(action);
            return state;
        }
    
        case ExampleActionType.SUCCESS : {
            console.log(action);
            return state.merge({fakeData: action.payload });
        }
    
        case ExampleActionType.FAILURE : {
            console.log(action);
            return state;
        }
    
        default:
            return state;
      }
    }
    

    I also had to export the symbols like this

    example-action.js

    export const REQUEST = Symbol('REQUEST');
    export const SUCCESS = Symbol('SUCCESS');
    export const FAILURE = Symbol('FAILURE');
    

    Conclusion

    Awesome library that gives you all the tools you need to creat api requests with very little code. Hope this helps someone who is confused about this like I was.