Reactjs 如何使用挂钩更新react中的数组。不添加新元素,只是在列表中循环
大家好,我有点困在一个小问题上。 我正在尝试使用Reactjs 如何使用挂钩更新react中的数组。不添加新元素,只是在列表中循环,reactjs,loops,react-hooks,Reactjs,Loops,React Hooks,大家好,我有点困在一个小问题上。 我正在尝试使用react hooks更新数组的状态 基本上我有一个清单 const hatList=[”您必须为当前索引使用状态 const [currentIndex, setCurrentIndex] = useState(0) // in onClickButton, increase this index <Button className="btn" btnText="Sho
react hooks
更新数组的状态
基本上我有一个清单
const hatList=[”您必须为当前索引使用状态
const [currentIndex, setCurrentIndex] = useState(0)
// in onClickButton, increase this index
<Button
className="btn"
btnText="Show Me Missing Koroks!"
onClick={() => setCurrentIndex((index) => index < hatList.length-1 ? index+1 : 0)}
/>
<>
const[currentIndex,setCurrentIndex]=useState(0)
//在onClickButton中,增加此索引
setCurrentIndex((index)=>index
要显示当前帽子,请执行以下操作:
<>
<span role="img" aria-label='hat emoji various'>{hatList[currentIndex] }</span>
<Icon name="korok" />
<Icon name="mount" />
</>
{hatList[currentIndex]}
谢谢@Sonia Aguilar这很有帮助。现在正在工作。