📅  最后修改于: 2022-03-11 15:02:51.393000             🧑  作者: Mango
const TagInput = ({ tags }) => {
const [tagData, setTagData] = React.useState(tags);
const removeTagData = indexToRemove => {
setTagData([...tagData.filter((_, index) => index !== indexToRemove)]);
};
const addTagData = event => {
if (event.target.value !== '') {
setTagData([...tagData, event.target.value]);
event.target.value = '';
}
};
return (
{tagData.map((tag, index) => (
-
{tag}
removeTagData(index)}
>
x
))}
(event.key === 'Enter' ? addTagData(event) : null)}
placeholder="Press enter to add a tag"
/>
);
};