【发布时间】:2018-10-12 11:17:14
【问题描述】:
UploadImage 组件是一个哑组件。它获取调用上传的 redux 操作,并获取图像文件路径,一旦图像启动,该操作将其放入适当的 reducer。这里它与两个道具一起使用:
<UploadImage onUpload={this.props.uploadNewArticleImage} image={this.props.newArticleBuffer.image}/>
我的newArticleBuffer-reducer 有image: null,上传完成后会得到图片的路径。发生这种情况时,我的组件会做出反应,不再显示微调器。
在内部,UploadImage 如下所示:
import React, { Component } from 'react';
export default class UploadImage extends Component {
constructor(props) {
super(props);
this.state = { file: "", loading: false};
this.onSubmit = this.onSubmit.bind(this);
this.onFileChange = this.onFileChange.bind(this);
}
onSubmit(e) {
e.preventDefault();
this.setState({loading: true});
this.props.onUpload(this.state.file);
}
onFileChange(event) {
this.setState({ file: event.target.files[0]});
}
render() {
let spinner = <div>Not Loading</div>
if(this.props.image == null && this.state.loading) {
spinner = <div>Loading</div>;
}
return (
<div>Image Upload Component
<input
type="file"
accept="image/*"
onChange={this.onFileChange} />
{spinner}
<button onClick={(e) => this.onSubmit(e)}>Upload Image</button>
</div>
);
}
}
有几个问题。首先,我从未将“加载”设置回false:当 Redux 操作完成时,我需要以某种方式执行此操作,但我不知道如何执行此操作。
其次,更重要的是,当用户上传图片但随后决定上传其他文件时,这种方法将不起作用。 image 将不再是 null。
【问题讨论】:
-
使用 redux-thunk
-
我已经是了。您认为这将如何解决问题?
标签: javascript reactjs redux react-redux