React-Redux - 没有为密钥提供减速器"硬币"

时间:2017-06-27 23:33:51

标签: javascript reactjs redux react-redux

不确定我为什么会收到以下错误。

我只是设置我的商店,行动和减速器,我还没有调用任何东西。

预期

App运行良好,Redux状态未更新

结果

enter image description here

SRC / index.js
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 /减速器/ index.js
import { combineReducers } from 'redux'
import { coins } from './coins'

export default combineReducers({
    coins
});
SRC /减速机/动作/ 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
import { ADD_COIN } from './actions'

const initialState = [];

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

5 个答案:

答案 0 :(得分:55)

您的问题在于您如何导入<xsl:copy>缩减器:

coins

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

您没有使用任何已命名的导出import { coins } from './coins' ,因此您只需按如下方式导入文件:

export default

使用后者将导致import coins from './coins'; 将包含coins的值;这将是硬币减速器。

答案 1 :(得分:6)

啊刚发现它,我正在错误地导入我的硬币减速器...

let names = ["item", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"],
    points = [12, 12345, 5765, 123, 3, 567765, 99, 87654, 881, 101],
    
    res = names.map((v, i) => ({ name: v, val: points[i] }))
               .sort((a, b) => b.val - a.val)
               .map(v => v.name);
    
    console.log(res);

而不是

import { combineReducers } from 'redux'
import coins from './coins' // because I have coins/index.js

export default combineReducers({
    coins
});

答案 2 :(得分:3)

即使正确导入了所有导入,也可能由于其他原因而发生这种情况。循环依赖!

在我的情况下,由于文件中存在循环依赖关系,因此对此进行了限制。我偶然创建的项目中有两个循环依赖项。例如:rootReducer.ts -> authSlice.ts -> rootReducer.ts

这些依赖项通常不那么容易调试。我使用this包检查循环依赖关系。除去循环依赖关系之后,一切都会好起来。

答案 3 :(得分:0)

这是我的解决办法

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

export default combineReducers({
    coinsState: coins || (() => null) // By adding this I resolved it.
});

答案 4 :(得分:0)

就我而言,我没有向减速器添加默认属性。当我添加它时,它起作用了。 这是我的代码;

const counterReducer = (state = 0, action) => {
    switch(action.type){
      case 'INCREMENT':
        return state + 1;
      case 'DECREMENT':
          return state - 1;
      default:
        return state;    
    }
  }
  
export default counterReducer;

和组合文件;

import   counter   from './counter';
import { combineReducers } from 'redux';

const allReducers = combineReducers({
    counter: counter,
   
});

export default allReducers;