Evan Bates
Evan Bates

Reputation: 31

Redux action not triggering reducers

Problem

I wired up my react application with a Redux store, added an api action to gather data from my backend including middleware redux-promise. Most everything seems to work as I can see my store in the React web editor along with the combine reducer keys. When I have my action called, it works and console logs the completed promise. However, my reducers never run. I thought it was an issue with my dispatch on the main container, but I've tried every way that I can think of at this point - regular dispatch() and bindActionCreators. HELP!

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import promiseMiddleware from 'redux-promise';
import RootReducer from './reducers';

const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore)

let store = createStore(RootReducer);

ReactDOM.render(
            <Provider store={store}>
                <App />
            </Provider>, 
            document.getElementById('root'));`

Combine Reducers

import { combineReducers } from 'redux';
import ReducerGetPostings from './reducer_get_postings'

const rootReducer = combineReducers({
    postingRecords: ReducerGetPostings
})

export default rootReducer;

Reducer

import { FETCH_POSTINGS } from '../actions/get_postings'

export default function (state = null, action) {
    console.log('action received', action)
    switch (action.type) {
        case FETCH_POSTINGS:
            return [ action.payload ]
    }
    return state;
}

Action API

import axios from 'axios';
import { url } from '../api_route';

export const FETCH_POSTINGS = 'FETCH_POSTINGS'

export function fetchpostings() {
    const postingRecords = axios.get(`${url}/api/postings`)

    console.log('Postings', postingRecords)
    return {
        type: FETCH_POSTINGS,
        payload: postingRecords
    };
}

Container

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'
import { fetchpostings } from '../../actions/get_postings.js'

class Dashboard extends Component {

    //....lots of other functionality already built here.

    componentDidMount() {
      axios.get(`${url}/api/postings`)
        .then(res => res.data)
        .then(
          (postingRecords) => {
            this.setState({
              postingData: postingRecords,
              postingOptions: postingRecords
            });
          },
          (error) => {
            this.setState({
              error
            })
          }
        )
    // primary purpose is to replace the existing api call above with Redux Store and fetchpostings action creator

        fetchpostings()
    }
}

function mapDispatchToProps(dispatch) {
   // return {actions: bindActionCreators({ fetchpostings }, dispatch)}
  return {
    fetchpostings: () => dispatch(fetchpostings())
  }
}

export default connect(null, mapDispatchToProps)(Dashboard);

Upvotes: 3

Views: 2184

Answers (1)

HMR
HMR

Reputation: 39250

You are not dispatching your action, when you call fetchpostings() in componentDidMount you are calling the method imported from actions/get_postings.js, not the method that will dispatch.

Try this.props.fetchpostings() instead.

You also did not bind state to props you need to do that as well.

Upvotes: 10

Related Questions