使用钩子重写react-tag-autocomplete

时间:2020-07-23 02:46:14

标签: reactjs

如何使用钩子重写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)}
                    />

1 个答案:

答案 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)}
                    />