【问题标题】:Add data in React object在 React 对象中添加数据
【发布时间】:2022-07-06 05:57:01
【问题描述】:

我希望你帮我解决一些对我来说效果不佳的事情,发生的情况是我想通过一个函数在数组中添加一个数据,但不是添加它,而是覆盖它,我不知道 redux 出了什么问题:

const [list,setList]=useState([])
const addList=(id)=>{
    setList([id])
}

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    我想你想要这样的东西,它将id 附加到list 数组:

    const [list,setList]=useState([])
    const addList=(id)=>{
        setList([...list, id])
    }

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 2019-03-16
      • 2016-09-01
      • 2018-07-16
      • 2012-10-09
      • 1970-01-01
      • 2019-07-24
      • 2020-05-10
      • 2023-02-15
      相关资源
      最近更新 更多