如何修复 redux-thunk 中的错误:需要赋值或函数调用,却看到了表达式

How to fix error in redux-thunk: Expected an assignment or function call and instead saw an expression

我正在尝试借助 redux-thunk 将图像发送到 firebase 商店。该代码在没有 redux 的情况下工作完美。

已编辑:这是错误的thunk ./src/Thunks/index.js


    // uploadReducer Thunk

    import {uploadImagesPending, uploadImagesProgress, uploadImagesSuccess, uploadImagesError} from '../Actions';
    import fire, { auth } from '../fire';
    // import  store  from '../index';

    function uploadImages(images) {
        return dispatch => {
            dispatch(uploadImagesPending());

            // const images = store.getState().images;
            const userID = fire.auth().currentUser.uid;
            const uploadTask = fire.storage().ref(`users/${userID}`).child(`images/${images.name}`).put(images);

            uploadTask.on('state changed', (snapshot) => {
                    const progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
                    dispatch(uploadImagesProgress(progress))
            }),
            (error) => {
                dispatch(uploadImagesError(error))
            },
            () => {
                fire.storage().ref('images').child(images.name).getDownloadURL().then(url => {
                    dispatch(uploadImagesSuccess(url))
                })
            }
        }
    }

    export default uploadImages;

简单的动作和reducer...

来自上传组件的图片

 handleChange(event) {
        if(event.target.files[0]) {
            const targetImages = event.target.files[0];

            this.setState({
                images: targetImages
            });
        }
    }
    uploadImage() {
        const { uploadImages } = this.props;
        const { images } = this.state;
        uploadImages(images);
    }

但是抓住了

编译失败。 ./src/Thunks/index.js 第 17 行:期望赋值或函数调用,却看到了一个表达式 no-unused-expressions 搜索关键字以了解有关每个错误的更多信息。

问题是您将 uploadTask.on 函数的 ) 放在了错误的位置。正确的应该是

uploadTask.on('state changed', (snapshot) => {
  const progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
  dispatch(uploadImagesProgress(progress));
},
(error) => {
  dispatch(uploadImagesError(error));
},
() => {
  fire.storage().ref('images').child(images.name).getDownloadURL()
    .then((url) => {
      dispatch(uploadImagesSuccess(url));
    });
});