首页 > 解决方案 > 调用动作函数的问题是 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 不是函数

一切都应该没问题。我尝试了很多方法,但结果都是一样的。谁能帮我?

标签: reactjsreact-reduxtypeerror

解决方案


我认为这只是this.props.addCart()


推荐阅读