Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

typescript - how to get and send data using reactjs and redux

I have problem when trying to send data through the function action in redux, my code is below

import React from 'react'
import {connect} from 'react-redux'
import {RetrieveCompany} from '../../folder/action/my.actions'

interface Icampaing{
   campaing: my_data
}

// campaing IS WORKING WELL, GET ME ALL MY DATA
const Personal: React.FC<Icampaing> = ({campaing}, props: nay) => {
    React.useEffect(()=>{
        let pf_id: any = campaing.profile ? campaing.profile.id : 0
        let pc_id: any = campaing.profile_ca

        // THE PROBLEM IS HERE SHOW ME THE ERROR
        // TypeError: props.RetrieveCompany is not a function
        props.RetrieveCompany(pf_id, pc_id)

    },[campaing])
     return(<>
         {campaing.all_data} // HERE WHEN LOAD DATA campaing WORKING WELL
     </>)

}
const mapStateToProps = (state: any) =>({
   campaing: state.campaing
})
const mapActionToProps = {
  RetrieveCompany
}
export default connect(mapStateToProps, mapActionToProps)(Personal)

please help me, I think forget something.

best words, and happy new year.....!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use mapDispatchToProps instead of mapActionToProps

const mapDispatchToProps = (dispatch) => ({
  RetrieveCompany: () => dispatch(RetrieveCompany())
  // Important: this could be just 
  // RetrieveCompany depends on how you define your action.
  // For naming, I would use camelCase
});

Because what you need to do here is to dispatch an action so that the store will update its state. Then you would read the data returned by mapStateToProps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...