【发布时间】:2018-08-14 13:12:57
【问题描述】:
import { Button, Form, FormGroup, Label, Input, FormText } from 'reactstrap';
export default class UserPicForm extends React.Component {
constructor() {
super();
// bindings
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
let file = this;
console.log('this is the file', file);
}
render() {
return (
<Form onSubmit={this.handleSubmit}>
<FormGroup>
<Label for="exampleFile">Upload Image</Label>
<Input ref={(ref) => this.fileUpload = ref} type="file" name="file" id="exampleFile" />
<FormText color="muted">
Please use the field above to upload your profile picture.
</FormText>
</FormGroup>
<Button type="submit">Upload</Button>
</Form>
);
}
}
我正在尝试上传一个文件并在表单提交时抓取它,我认为这是通过 ref 属性完成的,但是当我控制台记录我的“this”时,refs 属性为空。我做错了什么?
我还使用 React 检查器检查了我的 UserPicForm 组件,refs 部分也显示为“null”。
【问题讨论】:
标签: javascript reactjs