【发布时间】:2021-06-15 19:01:46
【问题描述】:
我想使用自定义 Refs 动态创建组件,并且我想使用这些 refs 并与 measure() 等功能一起使用。
我创建了以下代码
var elems = [];
const CarousalLoop = (props) => {
var links = [
{ name: 'america' },
{ name: 'canada' },
{ name: 'norway' },
{ name: 'bahamas' }
];
links.forEach((val, index) => {
elems[val.name]= useRef(null);
});
const listItems = links.map((link) =>
<CustomCarousel ref={elems[link.name]} category={link.name}/>
);
return (
<View style={{backgroundColor:"white"}}>
{listItems}
</View>
);
}
例如,我想使用 elems["america"] 访问组件并稍后使用它。 但我收到以下错误。
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
【问题讨论】:
标签: reactjs react-native react-hooks react-ref