元素交互
1.react-dropzone file 文件选取
npm install --save react-dropzone
import React from "react";
import { useDropzone } from "react-dropzone";
function Basic(props) {
const { acceptedFiles, getRootProps, getInputProps } = useDropzone();
const files = acceptedFiles.map((file) => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div {...getRootProps({ className: "dropzone" })}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
2. react-toastify
import React from "react";
import { toast, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
toast("🦄 Wow so easy!", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "light",
});
function Example() {
const notify = () => {
toast("Default Notification !");
toast.success("Success Notification !", {
position: toast.POSITION.TOP_CENTER,
});
toast.error("Error Notification !", {
position: toast.POSITION.TOP_LEFT,
});
toast.warn("Warning Notification !", {
position: toast.POSITION.BOTTOM_LEFT,
});
toast.info("Info Notification !", {
position: toast.POSITION.BOTTOM_CENTER,
});
toast("Custom Style Notification with css class!", {
position: toast.POSITION.BOTTOM_RIGHT,
className: "foo-bar",
});
};
return (
<>
<button onClick={notify}>Notify</button>;
<ToastContainer />
</>
);
}
3.react-onclickoutside
类组件
import React, { Component } from "react";
import onClickOutside from "react-onclickoutside";
class MyComponent extends Component {
handleClickOutside = evt => {
// ..handling code goes here...
};
}
export default onClickOutside(MyComponent);
函数组件
// 需要先设置函数
function listenForOutsideClicks(listening, setListening, menuRef, setIsOpen) {
return () => {
if (listening) return;
if (!menuRef.current) return;
setListening(true);
[`click`, `touchstart`].forEach((type) => {
document.addEventListener(`click`, (evt) => {
if (menuRef.current.contains(evt.target)) return;
setIsOpen(false);
});
});
}
}
// 组件内使用
import React, { useEffect, useState, useRef } from "react";
import listenForOutsideClicks from "./somewhere";
const Menu = () => {
const menuRef = useRef(null);
const [listening, setListening] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const toggle = () => setIsOpen(!isOpen);
useEffect(listenForOutsideClick(
listening,
setListening,
menuRef,
setIsOpen,
));
return (
<div ref={menuRef} className={isOpen ? "open" : "hidden"}>
<h1 onClick={toggle}>...</h1>
<ul>...</ul>
</div>
);
};
export default Menu;
4.interactjs
拖动/反馈相关