我无法理解它
actions.js
export const UPDATE = "UPDATE";
utils.js
export default function createReducer(initialState, handlers) {
return function reducer(state = initialState, action) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action)
} else {
return state
}
}
}
reducer.js
import createReducer from "../../lib/utils.js";
import * as actions from "./actions.js";
export const filter = createReducer({}, {
});
combinedReducers.js
import {
combineReducers
} from "redux";
import filter from "../container/filter/reducer.js";
export default combineReducers(Object.assign(
filter,
));
这就是我尝试连接store
。
index.android.js
import React, { Component } from "react";
import {
AppRegistry,
} from "react-native";
import {
Provider
} from "react-redux";
import {
createStore,
applyMiddleware,
compose,
} from "redux";
import {
createLogger,
} from "redux-logger";
import reducer from "./src/lib/reducers.js";
import thunkMiddleware from "redux-thunk";
import App from "./src/config/App.js"
const loggerMiddleware = createLogger({ predicate: (getState, action) => __DEV__});
function configureStore(initialState) {
const enhancer = compose(
applyMiddleware(
thunkMiddleware,
loggerMiddleware,
),
);
return createStore(reducer, initialState, enhancer);
}
const store = configureStore({});
const HelloWorld = () => {
<Provider store={store}>
<App/>
</Provider>
}
AppRegistry.registerComponent("...", () => HelloWorld);
我总是得到:
商店没有有效的减速机 确保传递给combineReducers的参数是一个值为reducers的对象。
我在这里做错了什么?我假设它是createReducer()
函数?!
答案 0 :(得分:1)
问题在于:
export default combineReducers(Object.assign(
filter,
));
filter
返回一个函数,Object.assign
返回该函数,combineReducers
期望一个对象,每个键都是一个reducer。
所以你的代码应该是这样的:
export default combineReducers(Object.assign({ filter } ));
或
export default combineReducers({ filter });
答案 1 :(得分:0)
中似乎有拼写错误
export default combineReducers(Object.assign, {
filter,
});
因为Object.assign
没有参数。
答案 2 :(得分:0)
这是我的减速机故障的一个例子
//reducers/index.js
import { combineReducers } from 'redux';
import MyReducer from "./MyReducer";
const rootReducer = combineReducers({
myProp: MyReducer
});
export default rootReducer;
//reducer/MyReducer.js
export default function MyReducer (state = null, action) {
switch (action.type) {
case 'MY_ACTION_TYPE':
return action.payload
}
return state;
}