How to properly assign a url as a prop to action c

2019-08-25 10:06发布

问题:

I'm using react-native-image-crop-picker along with react-native-fetch-blob to grab an image from the camera roll and then save it to a Firebase storage bucket.

class CreateForm extends Component {
  componentWillMount() {
   this.setState({ loading: false });
  }

  // RNFetchBlob and RNImageCropPicker working together to store an 
  // image in firebase, and then returning a url for that image.

  openImage() {
    this.setState({ loading: true });
    const Blob = RNFetchBlob.polyfill.Blob;
    const fs = RNFetchBlob.fs;
    window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
    window.Blob = Blob
    //const uid = "12345"

    ImagePicker.openPicker({
      width: 300,
      height: 300,
      cropping: true,
      mediaType: 'photo',
    }).then(image => {
      const imagePath = image.path;

      let uploadBlob = null;

     const storage = firebase.storage();
     const storageRef = storage.ref();
     const imageRef = storageRef.child('dp.jpg');
     const mime = 'image/jpg';
     fs.readFile(imagePath, 'base64')
       .then((data) => {
          //console.log(data);
       return Blob.build(data, { type: `${mime};BASE64` });
    })
       .then((blob) => {
          uploadBlob = blob;
          return imageRef.put(blob, { contentType: mime });
       })
      .then(() => {
          uploadBlob.close();
          return imageRef.getDownloadURL();
       })

Everything works perfectly until this bit of code:

      .then((url) => {
          console.log(url);
          this.props.entryUpdate({ prop: 'image', url });
    });
   });
  }

It logs the url just fine. But when I try to assign this url as a prop named 'image' to the action creator 'entryUpdate', the Firebase realtime database gets an undefined object and throws an error.

Here's the action creator 'entryUpdate':

 export const entryUpdate = ({ prop, value }) => {
   return {
    type: ENTRY_UPDATE,
    payload: { prop, value }

  };
 };

Without the 'image' prop, everything executes correctly. So I know that I'm not giving the action creator the right syntax somehow. The

this.props.entryUpdate({ prop: 'image', url });

line has to be just plain wrong. Any help would be so appreciated!

回答1:

The problem is you're destructuring the object being passed into the action creator incorrectly, ({ prop, value }) is basically grabbing the properties from the object being passed through. prop is correct because the object being passed as an argument has a prop key with 'image' as the value. value however is undefined because value is not a defined key on the object passed as an argument. Using url instead would be correct and should fix your issue:

export const entryUpdate = ({ prop, url }) => {
   return {
    type: ENTRY_UPDATE,
    payload: { prop, url } // or { prop, value: url } if you dont want to edit your dispatcher.

  };
 };


回答2:

This is what worked:

.then((url) => {
      const { image } = this.props;
      this.props.entryUpdate({ prop: 'image', value: url });

I had to declare { image } as this.props, as well as specifying

value: url