我有一个带有浏览按钮的页面,我试图将浏览的文件传递给子组件(<attachment />
组件),但是出于某种原因,<AttachedFile />
组件总是以{ {1}},我想念什么?
{}
页面:
Attachment
export default class Attachment extends React.Component {
state = {
attachment: {},
};
handleAddAttachment = file => {
this.setState({ attachment: file });
this.props.handleChange('attachment', file);
};
render() {
const { attachment } = this.state;
return (
<Fragment>
<div>
<div>
Do you have
<br />
something to <LineBreak />
show me?
</div>
<div css={attach}>Upload attachments here</div>
<AttachmentButton handleAddAttachment={this.handleAddAttachment} />
<AttachedFile attachment={attachment} />
</div>
</Fragment>
);
}
}
组件:
AttachmentButton
export default function AttachmentButton({ styles, handleAddAttachment }) {
const handleUpload = file => {
handleAddAttachment(file);
};
return (
<div css={styles}>
<div css={attachmentBox}>
<label css={customSelectButton}>
<input type="file" css={selectButton} onChange={e => handleUpload(e.target.files[0])} />
Select file
</label>
</div>
</div>
);
}
组件:
AttachedFile
答案 0 :(得分:0)
似乎您没有打handleAddAttachment
。因此,附件始终为{}
。