Redux 传递数据时如何编写mapDispatchToProps?

Redux 传递数据时如何编写mapDispatchToProps?,redux,Redux,我在和Redux打交道。我正在尝试向redux发送一个包含数据的操作-我可以发送该操作,并在没有它的情况下获得成功的响应,那么我如何重写代码,以便能够跨多个服务器发送数据呢 我正在使用RN图像选择器选择图像。一旦用户选择了一个图像(在getPhotoFromGallery函数中),它将被写入本地状态,然后该本地状态将被传递到redux中的函数分派中。我得到的错误是this.props.addImageToPost(imageData)不是函数 class MyForm extends React

我在和Redux打交道。我正在尝试向redux发送一个包含数据的操作-我可以发送该操作,并在没有它的情况下获得成功的响应,那么我如何重写代码,以便能够跨多个服务器发送数据呢

我正在使用RN图像选择器选择图像。一旦用户选择了一个图像(在
getPhotoFromGallery
函数中),它将被写入本地状态,然后该本地状态将被传递到redux中的函数分派中。我得到的错误是this.props.addImageToPost(imageData)不是函数

class MyForm extends React.Component {
    state = {
        imageData: null,
        caption: null,
        base64URI: null
    }

  //choose the photo.
  getPhotoFromGallery = () => {
    const { imageData } = this.state
    ImagePicker.launchImageLibrary({}, (response)  => {
      console.log('Response = ', response);

      if (response.didCancel) {
        console.log('User cancelled image picker');
      }
      else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      }
      else {
        //send image to redux
        this.setState({ imageData: response });
        this.setState({ base64URI: response.uri });
        this.props.addImageToPost(imageData) // ????
      }
    });
  };

  //show image in component
  showPickedImage() {
    const { imageData } = this.state;

    if (imageData !== null) {
        return (
              <Image
              source={{ uri: imageData.uri }}
              style={{ alignSelf: 'center', width: 90, height: 90, borderRadius: 20, marginLeft: 15 }}
              />
        );
    } else {
      return (
        <View>
          <TouchableOpacity
            style={{ alignSelf: 'center', width: 90, height: 90, borderRadius: 20, marginLeft: 15, alignItems: 'center', justifyContent: 'center', backgroundColor: 'white', shadowColor: "#000", shadowOffset: { width: 0, height: 5, }, shadowOpacity: 0.2, shadowRadius: 15, }}
            onPress={this.getPhotoFromGallery}
          >
            <Icon name="camera" size={32} color="black" />
          </TouchableOpacity>
        </View>
      );
    }
  }

  render() {
    const { image } = this.props;

    return (
        <View style={styles.myFormContainer}>
              {this.showPickedImage()}
        </View>
    );
  }

function mapStateToProps(state) {
    return { imageData: state.post.imageData }
}

const mapDispatchToProps = {
  addImageToPost, // ????
};

export default connect(mapStateToProps, mapDispatchToProps)(MyForm)
类MyForm扩展了React.Component{
状态={
imageData:null,
描述:空,
base64URI:null
}
//选择照片。
getPhotoFromGallery=()=>{
const{imageData}=this.state
ImagePicker.launchImageLibrary({},(响应)=>{
log('Response=',Response);
if(response.didconcel){
log('User cancelled image picker');
}
else if(response.error){
log('ImagePicker错误:',response.Error);
}
否则{
//将图像发送到redux
this.setState({imageData:response});
this.setState({base64URI:response.uri});
此.props.addImageToPost(imageData)/????
}
});
};
//在组件中显示图像
showPickedImage(){
const{imageData}=this.state;
如果(imageData!==null){
返回(
);
}否则{
返回(
);
}
}
render(){
const{image}=this.props;
返回(
{this.showPickeImage()}
);
}
函数MapStateTops(状态){
返回{imageData:state.post.imageData}
}
const mapDispatchToProps={
addImageToPost,//????
};
导出默认连接(mapStateToProps、mapDispatchToProps)(MyForm)
请参阅

应该是这样的

const mapDispatchToProps = dispatch => ({
  addImageToPost: (imageData) => dispatch({ type: 'ActionType', payload: imageData })
});
或者如果你有一个动作创造者

const mapDispatchToProps = dispatch => ({
  addImageToPost: (imageData) => dispatch(myActionCreator(imageData))
});
并像这样在组件中使用它

else {
    //send image to redux
    this.setState({ imageData: response });
    this.setState({ base64URI: response.uri });
    this.props.addImageToPost(imageData); // ???? correct
  }

谢谢。除了需要在mapDispatchToProps@JamesStirrat是的,应该使用括号而不是返回。上面编辑的答案应该是一样的。