gpt4 book ai didi

javascript - 在 React 中拖放图像

转载 作者:行者123 更新时间:2023-11-30 19:29:00 25 4
gpt4 key购买 nike

我正在尝试使用 React JS 和 react-dropzone 实现拖放行为显示缩略图的库。

代码如下:

import React from "react";
import ReactDOM from "react-dom";
import Dropzone from "react-dropzone";

import "./styles.css";

class App extends React.Component {
constructor(props) {
super(props);

this.state = {
dropzone1: [],
dropzone2: []
};
}

addFilesToDropzone(files, dropzone) {
let files_with_preview = [];
files.map(file => {
file["preview"] = URL.createObjectURL(file);
files_with_preview.push(file);
});

const new_files = [...this.state[dropzone], ...files_with_preview];
this.setState({ [dropzone]: new_files });
}

render() {

return (
<div className="App">
<Dropzone
onDrop={files => {
this.addFilesToDropzone(files, "dropzone1");
}}
>
{({ getRootProps, getInputProps }) => (
<div {...getRootProps()} className="">
<input {...getInputProps()} />
<div style={{ height: 100, backgroundColor: "yellow" }}>
Drop some files here
{dropzone1.map(file => (
<img
src={file.preview}
alt={file.path}
style={{ width: 40, height: 40 }}
/>
))}
</div>
</div>
)}
</Dropzone>

<div style={{ display: "flex", flexDirection: "row", marginTop: 25 }}>
<div style={{ width: "100%" }}>
DROPZONE 2
<Dropzone
onDrop={files => {
this.addFilesToDropzone(files, "dropzone2");
}}
>
{({ getRootProps, getInputProps }) => (
<div {...getRootProps()} className="">
<input {...getInputProps()} />
<div style={{ height: 100, backgroundColor: "yellow" }}>
Drop some files here
{this.state.dropzone2.map(file => (
<img
src={file.preview}
alt="dsds"
style={{ width: 40, height: 40 }}
/>
))}
</div>
</div>
)}
</Dropzone>
</div>
</div>
</div>
);
}
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Here is the example on codesandbox.

当我从计算机上的文件夹中拖动文件时,一切正常,但我希望能够将使用 dropzone 1 生成的缩略图拖到 dropzone 2。但这不起作用。

知道怎么做吗?

最佳答案

是的,那是行不通的,因为这不是 react-dropzone 的设计目的。引自网站,

Simple React hook to create a HTML5-compliant drag'n'drop zone for files.

改用 react-dnd 或 react-beautiful-dnd。

关于javascript - 在 React 中拖放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56631442/

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