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.