我使用了这样的钩子
const Ref1 = useRef();
const Ref2 = useRef();
const AllRefs = { Ref1, Ref2 };
是否可以简写一下呢?也许像这样:
const AllRefs = { Ref1:useRef(), Ref2:useRef()};
应该是这样吗?
import React, { useRef } from "react";
export default function App() {
const arr = [1, 2, 3];
const refs = useRef([]);
return (
<div className="App">
{arr.map((item, index) => {
return (
<div
key={index}
ref={(element) => {
refs.current[index] = element;
}}
>
{item}
</div>
);
})}
</div>
);
}