React-Redux - 没有为关键的“硬币”提供减速器。 [英] React-Redux - No reducer provided for key "coins"

查看:233
本文介绍了React-Redux - 没有为关键的“硬币”提供减速器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么我会收到以下错误。

Not sure why I'm getting the following errors.

我只是设置我的商店,行动和减速器,我没有打电话给任何事情然而。

I'm just setting up my store, actions and reducers, I haven't called dispatch on anything yet.

应用运行正常,Redux状态未更新

App runs fine, Redux state is not updated

import React from 'react'
import ReactDOM from 'react-dom'

import { createStore, applyMiddleware, compose } from 'redux'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import reducer from './reducer'

import App from './App'
import css from './coinhover.scss'

const element = document.getElementById('coinhover');

const store = createStore(reducer, compose(
    applyMiddleware(thunk),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
));

ReactDOM.render(
    <Provider store={ store }>
        <App />
    </Provider>, element);



src / reducer / index.js

src/reducer/index.js

import { combineReducers } from 'redux'
import { coins } from './coins'

export default combineReducers({
    coins
});



src / reducer / actions / coins.js

src/reducer/actions/coins.js

import * as api from '../../services/api'
import { storage, addToPortfolio } from '../../services/coinFactory'

export const ADD_COIN = 'ADD_COIN'

export function getCoin(coin) {
    return dispatch => {
        api.getCoin(coin)
            .then((res_coin)  => addToPortfolio(res_coin))
            .then((portfolio) => dispatch(updatePortfolio(portfolio)));
    }
}

export function updatePortfolio(portfolio) {
    return {
        type: ADD_COIN,
        portfolio
    }
}



最后src / reducer / coins / index.js

finally src/reducer/coins/index.js

import { ADD_COIN } from './actions'

const initialState = [];

export default (state = initialState, action) => {
    switch(action.type) {
        case ADD_COIN:
            return action.portfolio;
        default:
            return state;
    }
}


推荐答案

您的问题在于您如何导入 reducer:

Your issue lies with how you're importing your coins reducer:

import { coins } from './coins'

后者尝试获取从文件返回的命名导出in ./coins。

The latter tries to obtain a named export returned from the file in ./coins.

您没有使用任何命名导出导出默认,因此您只需要导入该文件如下:

You are not using any named exports only export default, therefore you just need to import the file as follows:

import coins from './coins';

使用后者将导致将包含 export default 的值;这将是硬币减速器。

Using the latter will result with the fact that coins will then contain the value of export default; which will be the coins reducer.

这篇关于React-Redux - 没有为关键的“硬币”提供减速器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆