gpt4 book ai didi

javascript - react-redux 上传文件错误

转载 作者:行者123 更新时间:2023-11-29 16:42:44 27 4
gpt4 key购买 nike

我尝试使用redux-form在react-redux上实现文件上传,但是在控制台有警告和异常:

警告:ConnectedField 正在将类型文件的不受控制的输入更改为 受控。输入元素不应从不受控切换到受控 (或相反亦然)。决定使用受控或非受控输入 组件生命周期的元素。

bundle.js:37467 Uncaught DOMException: Failed to set the 'value' property on 
'HTMLInputElement': This input element accepts a filename, which may only be
programmatically set to the empty string.
at Object.updateWrapper (http://localhost:8080/dist/bundle.js:37467:20)
at ReactDOMComponent.updateComponent (http://localhost:8080/dist/bundle.js:36891:23)
at ReactDOMComponent.receiveComponent (http://localhost:8080/dist/bundle.js:36846:10)
at Object.receiveComponent (http://localhost:8080/dist/bundle.js:6247:22)
at ReactCompositeComponentWrapper._updateRenderedComponent (http://localhost:8080/dist/bundle.js:35859:23)
at ReactCompositeComponentWrapper._performComponentUpdate (http://localhost:8080/dist/bundle.js:35829:10)
at ReactCompositeComponentWrapper.updateComponent (http://localhost:8080/dist/bundle.js:35750:12)
at ReactCompositeComponentWrapper.receiveComponent (http://localhost:8080/dist/bundle.js:35652:10)
at Object.receiveComponent (http://localhost:8080/dist/bundle.js:6247:22)
at ReactCompositeComponentWrapper._updateRenderedComponent (http://localhost:8080/dist/bundle.js:35859:23)

这是我的组件的代码:

import React,{Component} from 'react';
import {Field, reduxForm} from 'redux-form';

class UploadFileForm extends Component {

onFormSubmit(data) {
console.log(data);
};

render() {
return (
<form role="form" onSubmit={this.props.handleSubmit(this.onFormSubmit)}>
<div className="form-group">
{/*<input name="file" type="file" className="file file-loading" data-allowed-file-extensions='[".owl"]'/>*/}
<Field name="owl-file" component="input" type="file" ref="owl-file-ref"/>
<label className="choose-file-info"></label>
</div>
<button type="submit" className="btn btn-primary">Upload</button>
</form>
);
}
}

export default reduxForm({
form: 'upload'
})(UploadFileForm);

最佳答案

以下对我有用;

const UploadFile = ({ input: {value: omitValue, ...inputProps }, meta: omitMeta, ...props }) => (
<input type='file' {...inputProps} {...props} />
);

然后我就这样用了;

<Field component={UploadFile} name='file' accept='.jpg' />

解决方案位于: https://github.com/erikras/redux-form/issues/1989

关于javascript - react-redux 上传文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com