【发布时间】:2016-07-26 02:32:10
【问题描述】:
第一次使用 Redux,我了解 Actions 和 Reducers 的概念,但无法让它们在我的代码中很好地发挥作用,我担心这是因为我使用的是 CoffeeScript 而不是 ES6。
我认为问题是我不能在我的 /app/reducers/index.coffee 中使用export default ...,但老实说 Redux 对我来说很新,而且我一直在与 webpack 一起磕磕绊绊,反应了一段时间,但仍然不是最舒服的,所以¯\_(ツ)_/¯
我不断收到错误:
Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.
/app/actions/index.coffee:
{ FETCH_ITEMS, FETCH_SINGLE_ITEM } = require('../constants')
fetch_items = ->
{ type: FETCH_ITEMS }
fetch_single_item = (id) ->
{
type: FETCH_SINGLE_ITEM,
id: id
}
module.exports = { fetch_items, fetch_single_item }
/app/reducers/items.coffee:
$ = require('jquery')
{ FETCH_ITEMS, FETCH_SINGLE_ITEM } = require('../constants')
{ fetch_items } = require('../actions')
INITIAL_STATE = []
items = (state=INITIAL_STATE, action) ->
console.log action.type
switch action.type
when FETCH_ITEMS
console.log 'FETCH_ITEMS'
$.get "http://dosomething.com/items", (resp) ->
console.log resp
when FETCH_SINGLE_ITEM
console.log 'do something'
else
console.log "suckit turk!"
state
module.exports = { items }
/app/reducers/index.coffee:
{ combineReducers } = require('redux')
items = require('./items')
reducers = combineReducers({ items })
module.exports = { reducers }
最后是 /app/index.cjsx
React = require("react")
ReactDOM = require("react-dom")
{
createStore,
combineReducers,
appyMiddleware
} = require('redux')
{ reducers } = require("./reducers/")
STORE = createStore combineReducers({ reducers, routing: routerReducer })
HISTORY = syncHistoryWithStore(browserHistory, STORE)
STORE.subscribe () ->
console.log STORE.getState()
...omitted react render: ->...
编辑
对不起大家,是的 routeReducer 已定义,我只是省略了它,因为我很确定这不是问题。
/app/index.cjsx
React = require("react")
ReactDOM = require("react-dom")
{
createStore,
combineReducers,
appyMiddleware
} = require('redux')
{
syncHistoryWithStore,
routerReducer
} = require('react-router-redux')
{ reducers } = require("./reducers/")
STORE = createStore combineReducers({ reducers, routing: routerReducer })
HISTORY = syncHistoryWithStore(browserHistory, STORE)
STORE.subscribe () ->
console.log STORE.getState()
...omitted react render: ->...
【问题讨论】:
-
index.coffee或index.cjsx中是否抛出此错误?另外,index.cjsx中的routerReducer定义在哪里? -
CoffeeScript 从来都不是一个好主意,现在是一个更糟糕的主意:/
-
感谢 @mash 没有任何帮助
标签: javascript coffeescript ecmascript-6 redux