2017-07-30 53 views
0

我正在使用Redux-thunk中间件处理React-Redux应用程序。我收到一个错误,指出:“类型错误:调度是不是一个函数”当我尝试在我actions.js文件执行名为removeStock()函数:TypeError:调度在actions.js文件中不是函数

action.js

export const deletePinnedStock = (user_id, stock_name, stock_id) => { 
    return dispatch => { 
    ApiService.delete("https://stackoverflow.com/users/" + user_id + "/stocks/" + stock_id) 
    .then(response => { 
     dispatch(removeStock(stock_name)) 
     console.log('here is the', response) 
    }).catch((errors) => { 
     console.log(errors) 
    }) 
    } 
} 

removeStock()看起来是这样的:

export const removeStock = (stock_name) => { 
    return { 
    type: 'REMOVE_PINNED_STOCK', 
    stock_name: stock_name 
    } 
} 

它对应于我的减速器 'REMOVE_PINNED_STOCK' 采取行动的情况下,声明是这样的:

reducer.js

case 'REMOVE_PINNED_STOCK': 
    return { 
     ...state, 
     stocksData: { 
     ...state.stocksData.delete((stock) => stock.name === action.stock_name) 
     } 
    } 

我不知道为什么我无法将deletePinnedStock()函数中派遣removeStock()函数。在我的action.js文件中的其他位置,我没有问题调度函数。

编辑#1: deletePinnedStock在我的组件定义如下:

stockCard.js

import React, { Component } from 'react'; 
import { connect } from 'react-redux'; 
import { fetchPinnedStocks, deletePinnedStock, fetchStockData } from 
'../redux/modules/Stock/actions'; 
import '../styles/spin.css'; 
import Panel from 'react-uikit-panel'; 

class StockCard extends Component { 

render() { 
    const user_id = localStorage.getItem('currentUser_id') 
    const stock = this.props.stock //should be the stockObj keyed by name 
    if (!stock) { 
     return null 
    } else { 
    return (
     <Panel col='1-2' box title={stock.name} margin='bottom' context='primary'> 
      <div> 
       Open: {stock.openingPrice} 
      </div> 
      <div> 
       Close: {stock.closingPrice} 
      </div> 
      <div> 
       Low: {stock.low} 
      </div> 
      <div> 
       High: {stock.high} 
      </div> 
      <div> 
       Trading Volume: {stock.volume} 
      </div> 
      <button type="submit" 
      onClick={deletePinnedStock(user_id, stock.name, stock.id)}>Remove</button> 
     </Panel>) 
    } 
    } 
} 

function mapStateToProps(state) { 
    return { 
    currentUser: state.auth.currentUser, 
    stocksData: state.stock.stocksData 
    } 
} 

export default connect(mapStateToProps, { fetchPinnedStocks, 
deletePinnedStock, fetchStockData })(StockCard); 
+0

硬盘更换

onClick={deletePinnedStock(user_id, stock.name, stock.id)} 

从提供什么可讲。如何在组件中设置deletePinnedStock? – thgaskell

+4

[Redux-thunk:\'dispatch可能不是函数的重复](https://stackoverflow.com/questions/44265007/redux-thunk-dispatch-is-not-a-function) –

回答

1

通过直接调用deletePinnedStock,你只是调用它的函数,而不是将其分发到redux商店。当你将动作创建者传递给connect()时,它会作为道具添加到组件中,并且该道具将映射到dispatch

总之,随着

onClick={this.props.deletePinnedStock(user_id, stock.name, stock.id)} 
+0

谢谢很多 - 这是完全正确的! –

相关问题