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

问题描述

不知道为什么会出现以下错误.

Not sure why I'm getting the following errors.

我只是在设置我的 store、actions 和 reducer,我还没有调用 dispatch.

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;
    }
}

推荐答案

你的问题在于你如何导入你的 coins 减速器:

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

import { coins } from './coins'

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

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

您没有使用任何命名导出仅 export default,因此您只需要按如下方式导入文件:

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

import coins from './coins';

使用后者将导致 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 - 没有为关键的“硬币"提供减速器;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 23:54