reactjs - 有谁知道如何实现 useDropzone 钩子而不是(组件)连同 react-final-form
问题描述
例如,如果我有
const ImageInput = ({fieldPropsFromFinalForm}) => {
const { getRootProps, getInputProps, open } = useDropzone({ onDrop, accept: 'image/jpeg, image/png' });
.
.
.
return (<input {...getInputProps()} {...fieldPropsFromFinalForm} /> )
}
这种方式输入并不明显,但我不知道如何使它工作如何一起处理它们。我是新手,我希望我没有问错问题。
解决方案
您必须onChange
在回调fieldPropsFromFinalForm
中调用onDrop
例如,请找到这个沙箱
请找到<ImageInput/>
组件的代码
图像输入
const ImageInput = props => {
const onDrop = useCallback(acceptedFiles => {
// Do something with the files
props.input.onChange(acceptedFiles);
}, []);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: "image/jpeg, image/png"
});
return (
<div {...getRootProps()}>
<input {...getInputProps()} {...props} />
{isDragActive ? (
<p>Drop the files here ...</p>
) : (
<p>Drag 'n' drop some files here, or click to select files</p>
)}
{props.input.value
? props.input.value.map(file => {
return <div>{file.path}</div>;
})
: null}
</div>
);
};
样本表格
function SampleForm() {
const onSubmit = values => {
alert(JSON.stringify(values));
};
return (
<div>
<Form onSubmit={onSubmit}>
{props => (
<form onSubmit={props.handleSubmit}>
<Field name="myField">
{props => (
<div>
<ImageInput {...props} />
</div>
)}
</Field>
<button type="submit">Submit</button>
</form>
)}
</Form>
</div>
);
}
推荐阅读
- vue.js - 未捕获的 ReferenceError: __VUE_HMR_RUNTIME__ 未定义
- neo4j - 关系加权neo4j
- c# - 如何将 UpdateProgress 用于文档类型请求?
- css - 如何将新的子元素和滚动条附加到父 div 的底部?
- python - 使用for循环从列表中提取2个值
- while-loop - denrometeR 包的函数 phase_def 在 r 中不起作用
- arcore - 三星 Galaxy S20 - 支持 ARCore 的设备,但在 Web AR 中打开 3D 时显示黑屏
- python - 关于pymunk准确性的几个问题
- javascript - 实现一个函数,它接受 2 个整数并返回它们之间(包括)它们之间的每个数字的总和
- google-ads-api - AW API 的查询生成器?