React Native Error: undefined is not a function (evaluating

On writing my first RN application, I have got the below error message on executing the code,

“undefined is not a function (evaluating ‘_ConfigureStore2.default.dispatch(CategoryAction.categoryView())’)”

ConfigureStore.js:

import {createStore, applyMiddleware} from 'redux';
import reducers from '../reducers';
import thunk from 'redux-thunk';

var middlewares = applyMiddleware(thunk);

export default function configureStore(initialState) {
  return createStore(reducers, initialState, middlewares);
}

CategoryContainer.js:

import React, { Component } from 'react';
import stores from '../stores/ConfigureStore';
import * as CategoryAction from '../actions/CategoryAction';

stores.dispatch(CategoryAction.categoryView());

class CategoryContainer extends Component {
}

CategoryAction.js:

import * as actionTypes from './ActionTypes';
import AppConstants from '../constants/AppConstants';

export function categoryView() {
  const categories = ['CATEGORY1', 'CATEGORY2'];
  return {
      type: "CATEGORY_VIEW",
      categories: categories
  };
}

CategoryReducer.js:

const initialState = {
  categories:[],
}

export default function categoryReducer (state = initialState, action) {
  switch (action.type) {
    case CATEGORY_VIEW:
      return Object.assign({}, state, {
              categories: action.categories
            });
    }
}

Even i tried the below approach in CategoryContainer.js, but still got the same error,

import { categoryView } from '../actions/CategoryAction';
stores.dispatch(categoryView());

Kindly assist to solve the issue.

Answer

On changing the ConfigureStore.js as below solves the issue.

import {createStore, applyMiddleware} from 'redux';
import reducers from '../reducers';
import thunk from 'redux-thunk';

/*var middlewares = applyMiddleware(thunk);
export default function configureStore(initialState) { //default is    undefined
  return createStore(reducers, initialState, middlewares);
}*/

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(reducers);
export default store;

Leave a Reply

Your email address will not be published. Required fields are marked *