我想创建一个像测验一样的程序。场景是用户将提交答案,答案将存储在数组中。变量数组就是答案。
this.state = { answer : [] }
用户将给出答案并存储答案。也许值会像['a','b','c']这只是一个示例答案将存储在数组中。不,测验的功能可以显示上一个或下一个问题,用户可以更改数组上的答案。如何更改特定索引中数组中的值?例如,数组第二个索引中的答案 b 将更改为 a。我怎样才能改变价值?
// Use react setState callback to ensure you get the updated state value
this.setState((state) => {
// Create new array to prevent passing reference to make it pure
const newAnswer = [...state.answer]
// Chage value of new array
newAnswer[index] = newValue
return {answer: newAnswer}
}
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句