ReactJS中useEffect挂钩内索引计数器递增的问题

ReactJS中useEffect挂钩内索引计数器递增的问题,reactjs,use-effect,use-state,Reactjs,Use Effect,Use State,在这部分代码中,我尝试获取一个状态变量“lastIndex”,并尝试使用setlastIndex对其进行递增,但lastIndex的值不会递增,而是保持为0。我是新手,请帮帮我。如果在堆栈溢出之前已经询问过这样一个精确的查询,那么请发送链接 function App() { const [myAppointment, setmyAppointment] = useState([]) **const [lastIndex, setlastIndex] = useState(0)**

在这部分代码中,我尝试获取一个状态变量“lastIndex”,并尝试使用setlastIndex对其进行递增,但lastIndex的值不会递增,而是保持为0。我是新手,请帮帮我。如果在堆栈溢出之前已经询问过这样一个精确的查询,那么请发送链接

function App() {
  const [myAppointment, setmyAppointment] = useState([])
  **const [lastIndex, setlastIndex] = useState(0)** 
  const [orderBy, setorderBy] = useState('petName')
  const [orderDir, setorderDir] = useState('asc')
  const [queryText, setqueryText] = useState('')
  useEffect(() => {
      fetch('./data.json')
      .then(response=> response.json())
      .then(result=>{
        **const apts=result.map(item=>{
          item.aptId = lastIndex
          setlastIndex(lastIndex => lastIndex+1)
          return item
        })**
        setmyAppointment(apts)
      })
  },[])

您应该了解,
lastIndex
不会在每次调用
setlastIndex
函数时增加,所以只需在循环外声明它,并在每次分配给aptId属性时增加。循环结束后,使用最后一个值设置状态

then(result=>{
  let currentIndex = lastIndex
  result.forEach(item=>{
    item.aptId = ++currentIndex
  })
  setmyAppointment(result)
  setlastIndex(currentIndex)
})

map
是不需要的,您可以使用result将其保存在
setmyAppointments

中。您应该了解,
lastIndex
不会在每次调用
setlastIndex
函数时都增加,所以只需在循环外声明它,并增加每次分配给apid属性的次数。循环结束后,使用最后一个值设置状态

then(result=>{
  let currentIndex = lastIndex
  result.forEach(item=>{
    item.aptId = ++currentIndex
  })
  setmyAppointment(result)
  setlastIndex(currentIndex)
})

map
不需要,您可以使用result将其保存在
setmyappoints

我也尝试过使用item.aptId=setlastIndex(lastIndex=>lastIndex+1)但这也不能增加我的lastIndex,实际上我有一个data.json文件,其中没有提到id,所以我想为data.json文件中的每个对象创建唯一的id。为此,我编写了此setLastIndex代码,但它只为data.json文件中的所有对象提供索引0。您好,欢迎使用SO!我认为您不应该在映射中使用useState钩子,您应该在映射中构建新的状态值(在您的示例中,Array.reduce可能是更好的选择),然后只调用一次setlastIndex:setlastIndex(newValue)。您可以先将其console.log以确保发生了什么。如何使用Array.reduce?从这里开始:我还尝试了执行item.aptId=setlastIndex(lastIndex=>lastIndex+1)但这也不能增加我的lastIndex,实际上我有一个data.json文件,其中没有提到id,所以我想为data.json文件中的每个对象创建唯一的id。为此,我编写了此setLastIndex代码,但它只为data.json文件中的所有对象提供索引0。您好,欢迎使用SO!我认为您不应该在映射中使用useState钩子,您应该在映射中构建新的状态值(在您的示例中,Array.reduce可能是更好的选择),然后只调用一次setlastIndex:setlastIndex(newValue)。您可以先将其console.log以确保发生了什么。如何使用Array.reduce?从这里开始:我希望为添加到列表中的新项创建新索引,但使用lastIndex并不能为使用表单创建的每个新对象提供新索引。索引冲突再次出现,如果你能帮我解决,我该怎么办?
const addAppointment=(apt)=>{let tempApt=myAppointment apt.apid=lastIndex tempApt.unshift(apt)setmyAppointment([…tempApt])}
@GazalJain,但你不增加lastIndex,您应该增加它,并将结果分配给您的peoperty和change State。我试图使用apt.aptinex=lastIndex+1增加lastIndex,但它只增加一次……但我希望在将新约会对象添加到现有数组时动态增加。@GazalJain,您应该将其保存在状态alsoI我希望为添加到列表中的新项目创建新索引,但使用lastIndex并不能为使用表单创建的每个新对象提供新索引。索引冲突再次出现,如果你能帮我解决,我该怎么办?
const addAppointment=(apt)=>{let tempApt=myAppointment apt.apid=lastIndex tempApt.unshift(apt)setmyAppointment([…tempApt])}
@GazalJain,但你不增加lastIndex,您应该增加它,并将结果分配给您的peoperty和change state我试图使用apt.aptIndex=lastIndex+1增加lastIndex,但它只增加一次….但我希望在将新约会对象添加到现有数组时动态增加。@GazalJain,您也应该将其保存在state中