使用 useState 挂钩更新数组时遇到问题。我想要的是我有一个填充了 n 个元素的数组,其中 n 可以是 1-10。我想使用它的索引号更新一个元素的值。但我没有得到任何帮助。这是我想要实现的代码:
import React, {useState} from 'react';
import React, {View, Text} from 'react-native';
const myScreen = props => {
const [myCustomArray, setmyCustomArray] = useState(Array.(5).fill(0));
const onClickHandler = (n) => {
// update the element in array having the index no. n
setMyCustomArray([n] = 'something');
}
但是这种方法对我没有帮助。有人有更好的方法吗?