Push a new value to nested array in React hooks

前端 未结 2 1759
我寻月下人不归
我寻月下人不归 2021-01-27 03:16

I have a function onTagSelected() that updates now a single value for a key inside a object:

const NotesContainer = ({

}) => {
  const [notesDummyData, setNo         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-27 03:50

    spread the previous tag and add selectedTag to end

    just change this

    return { ...x, tag: selectedTag };
    

    to

    return { ...x, tag: [...x.tag, selectedTag] };
    

    Or

    return { ...x, tag: x.tag.concat(selectedTag) };
    

提交回复
热议问题