💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
在upload中文件上传到后台,返回文件链接地址,但是在接受到地址之后,更新state组件没有响应更新。 对于受控模式,你应该在`onChange`中始终 setState`fileList`,保证所有状态同步到 Upload 内。类似这里的写法:[http://ant.design/components/upload/#components-upload-demo-fileList](http://ant.design/components/upload/#components-upload-demo-fileList) ~~~js // good onFileChange(fileList) { if ( ... ) { ... } else { ... } // always setState this.setState({ fileList: [...fileList] }); } ... <Upload fileList={this.state.fileList} onChange={this.onFileChange} /> ~~~ ~~~js // bad onFileChange(fileList) { if ( ... ) { this.setState({ fileList: [...fileList] }); } else { ... } } ... <Upload fileList={this.state.fileList} onChange={this.onFileChange} /> ~~~ 在 React.PureComponent 里面还是有问题的 因为React.PureComponent里面做了一层优化判断,filelist有缓存引用, 认为是同一个对象就不会更新, 在React.PureComponent立马 应该生成一个新的对象 handleChange = ({ fileList }) => { // fixed bug this.setState({ fileList: fileList.slice() }); }