在编写我的第一个RN应用程序时,我在执行代码时收到了以下错误消息,
"未定义不是函数(评估'_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);
}
类别container.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
};
}
类别Reducer.js:
const initialState = {
categories:[],
}
export default function categoryReducer (state = initialState, action) {
switch (action.type) {
case CATEGORY_VIEW:
return Object.assign({}, state, {
categories: action.categories
});
}
}
即使我尝试了cateroryContainer.js中的下面方法,但仍然有相同的错误,
import { categoryView } from '../actions/CategoryAction';
stores.dispatch(categoryView());
请帮助解决问题。
给它拍摄:
categoryAction.js
export default class CategoryAction {
categoryView = () => {
const categories = ['CATEGORY1', 'CATEGORY2'];
return {
type: "CATEGORY_VIEW",
categories: categories
};
}
}
categoryContainer.js
import React, { Component } from 'react';
import stores from '../stores/ConfigureStore';
import CategoryAction from '../actions/CategoryAction';
stores.dispatch(CategoryAction.categoryView());
class CategoryContainer extends Component {
}
在更改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) { //default is undefined
return createStore(reducers, initialState, middlewares);
}*/
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(reducers);
export default store;