1 回答
TA贡献1852条经验 获得超1个赞
这是根据 data.lenght 创建 useRef 的方法
import { useEffect, useRef, useState } from react;
const ArrayInputs = () => {
const inputRef = useRef([]);
const [data, setData] = useState([]);
useEffect( () => {
let data = ['Name', 'Age', 'Gender'];
inputRef.current = new Array(data.length);
setData(data);
}, []);
useEffect( () => {
//Example of using inputRef
if(data.length !== 0) {
inputRef.current[data.length - 1].focus();
}
}, [data]);
return(
<View>
{data.map( (element, i) => <TextInput ref = {el => inputRef.current[i] = el} />)}
</View>
);
}
添加回答
举报