reactjs - 调用动作函数的问题是 redux 我有一条消息:this.props.nameofprops.action() is not a fction
问题描述
我正在编写一个 react-redux 代码,我定义了一个要在组件中调用的操作,它称为 addCart。
import axios from "axios"
import {GET_PLATS} from "./actionType"
export const getplats = () => dispatch => {
// 2-1 axions get the same path of back in app.use
axios.get("/plat-list").then(res => {
dispatch({
//2-2 same name in action type (after this go to make reducers)
type:GET_PLATS,
payload:res.data
})
})
}
export const addCart =( ) =>{
return (dispatch) => {
console.log("added To cart");
dispatch({
type:GET_PLATS,
})
}
}
然后我写了这个减速器:
import { GET_PLATS } from "../action/actionType"
//first create first main state
const initialState={
plats:[],
cmdElements:[]
}
export default function(state=initialState,action){
switch(action.type){
case GET_PLATS:
return{
...state,
plats:action.payload,
cmdElements:state.cmdElements
}
default :
return state
}
}
然后我在一个组件中调用了这个动作**
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {getplats,addCart } from '../../action/action'
import PropTypes from 'prop-types'
const { Meta } = Card
class PlatListeU extends Component {
componentDidMount(){
this.props.getplats()
}
render() {
return (
<div>
<Button type="primary" onClick={ () =>{
this.props.platListe.addCart()
} } >addTocart</Button>
</div>
)}
PlatListeU.propTypes = {
addCart:PropTypes.func.isRequired,
PlatListe:PropTypes.object.isRequired
}
const mapStateToProps =(state) =>{
return{
platListe:state.plats ,
cmdElements:state.cmdElements
}
}
export default connect(mapStateToProps,{ getplats,addCart}) (PlatListeU)
但是当我按下按钮时,我收到以下错误消息:
TypeError:this.props.platListe.addCart 不是函数
一切都应该没问题。我尝试了很多方法,但结果都是一样的。谁能帮我?
解决方案
我认为这只是this.props.addCart()
推荐阅读
- c# - 如何在 SDK V3 上对 Cosmos-db 查询异步运行 LINQ Count
- haskell - 我如何才能纯粹地对异常进行分类?
- cookies - 在购物和结账之间存储变量
- css - 如何在桌子周围均匀地添加边框?
- r - 使用 data.table 在指定关键字之间提取字符串的第一个实例的最佳方法
- sql - 在 Django 中,如何从列表中构建一组标准?
- angular - 有没有办法为服务器端分页启用 MatPaginator 下一个/上一个按钮?
- r - 使用R计算基于多列的列中重复实验结果值的平均值
- swift - SKPaymentQueue.default().add(payment) 被忽略
- python - Keras 可视化工具包抛出`
输入:0 既被馈送又被获取。对于简单的“顺序”模型