【发布时间】:2017-03-13 07:23:12
【问题描述】:
我收到一条错误消息,但我不知道如何解决它:
Uncaught Error: Expected the reducer to be a function
以下是我的 react/redux 工作流程的各个部分:
根减速器
import { combineReducers } from 'redux';
import PostsReducer from './reducer_posts';
import StreakReducer from './reducer_streak';
import { reducer as formReducer } from 'redux-form';
const rootReducer = combineReducers({
entries: PostsReducer,
form: formReducer,
streak: StreakReducer
});
export default rootReducer;
动作创建者
import axios from 'axios';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index';
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);
export const FETCH_ENTRIES = 'FETCH_ENTRIES';
export const FETCH_LONGEST = 'FETCH_LONGEST';
const ROOT_URL = 'http://localhost:3000/entries';
export function fetchEntries() {
const request = axios.get(`${ROOT_URL}`);
return {
type: FETCH_ENTRIES,
payload: request
};
}
// action creator for returning the longest streak of rejections
export function longestStreak() {
return function(dispatch) {
return fetchEntries().then(
entries => dispatch(findLongestStreak(entries))
);
};
}
function findLongestStreak(entries) {
var longestStreakLength = 10;
return {
type: FETCH_LONGEST,
payload: longestStreakLength
};
}
条纹减少器
import { FETCH_LONGEST } from '../actions/index';
const INITIAL_STATE = { streak: 0 };
export default function(state = INITIAL_STATE, action) {
switch(action.type) {
case FETCH_LONGEST:
return { ...state, streakInfo: action.payload.data};
default:
return state;
}
}
帖子缩减器
import { FETCH_ENTRIES, CREATE_ENTRY } from '../actions/index';
const INITIAL_STATE = { all: [], entry: null };
export default function(state = INITIAL_STATE, action) {
switch(action.type) {
case CREATE_ENTRY:
return {...state, entry: action.payload.data };
case FETCH_ENTRIES:
return { ...state, all: action.payload.data };
default:
return state;
}
}
组件
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { longestStreak } from '../actions/index';
class EntriesMetadata extends Component {
componentWillMount() {
this.props.longestStreak();
}
render() {
return (
<div>
</div>
);
}
}
function mapStateToProps(state) {
return { streak: state.streak.streakInfo };
}
export default connect(mapStateToProps, { longestStreak })(EntriesMetadata);
我不知道如何解决这个问题。谁能指出我正确的方向?这是我的第一个 redux 项目!如果需要任何额外的 sn-ps,请告诉我。
编辑
这是我的 github 仓库:https://github.com/pchung39/courage
【问题讨论】:
-
你能显示两个reducer的代码吗?
-
请同时发布其他减速器的代码。如果您可以分享github链接,您可能会得到更快的答案。
-
@HazardouS 我在我的原始帖子上发布了这个项目的 github 链接。
标签: javascript reactjs redux redux-thunk