我不能撒谎,我对 react-redux 有点困惑。我认为很多操作都需要参数(例如从商店中删除项目),但即使我仍在阅读有关如何以这种方式从组件分派以传递参数的信息,现在大约 2 小时,我也没有得到任何答案。我尝试过this.props.dispatch和mapDispatchToProps,我总是收到this.props... is not a function消息。这是我想要做的:
  const getTheArray = (array) => {
    return {
      type: 'GET_ARRAY',
      array
    }
  }
    
    class Example extends......
    
    componentDidUpdate(){
    //i have a button, and when it clicked, turns the status: 'deleted'
        if (this.state.status === 'deleted'){
            fetch('http://localhost:3001/deleteitem', {
                method: 'post',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({
                    id: this.props.id
                })
            })
            .then(res => res.json())
            .then(data => this.props.deleteFromArray(data))
            .catch(err => console.log(err)) 
        }
    }
function mapStateToProps(state) {
    return {
        array: state.array
    };
}
function mapDispatchToProps(dispatch) {
    return({
        deleteFromArray: (array) => {dispatch(getTheArray(array))}
    })
}
  
export default connect(mapStateToProps, mapDispatchToProps)(Example);
这不是我需要用动作调度的唯一地方,动作对象的属性取决于传递给函数的另一个属性,所以我真的很想做,将属性传递给动作并调度它的最佳方法是什么在react组件中。