如何使用钩子重写react-tag-autocomplete?我遇到的每个示例都在onAddition方法中使用.bind(this)关键字,这是旧方法,我想对带有钩子的react-tag-autocomplete使用。我定义了以下内容:
const [tags, setTags] = useState([{ name: "tag1" }, { name: "tag2" }]);
const handleAddition = (e) => {
console.log("add");
setTags([...tags, tag]);
};
<ReactTags
tags={tags}
ref={reactTags}
onDelete={handleDelete}
//onAddition={handleAddition}
onAddition={(e) => handleAddition(e.target.value)}
/>
答案 0 :(得分:0)
更改此
const [tags, setTags] = useState([{ name: "tag1" }, { name: "tag2" }]);
const handleAddition = (tag) => {
console.log("add");
setTags([...tags, tag]);
};
<ReactTags
tags={tags}
ref={reactTags}
onDelete={handleDelete}
//onAddition={handleAddition}
onAddition={(e) => handleAddition(e)}
/>