React native 无法为函数组件提供引用
我有以下问题: 我有ActionWeekModel,它包含我自己的自定义组件MList。在此MList中有一个。我想在ActionWeekModel中使用SwipeListView的引用来触发函数scrollToEnd()。这应该是可能的,因为SwipeListView从FlatList继承了此函数。但是,我得到的错误如下:React native 无法为函数组件提供引用,react-native,react-functional-component,create-ref,React Native,React Functional Component,Create Ref,我有以下问题: 我有ActionWeekModel,它包含我自己的自定义组件MList。在此MList中有一个。我想在ActionWeekModel中使用SwipeListView的引用来触发函数scrollToEnd()。这应该是可能的,因为SwipeListView从FlatList继承了此函数。但是,我得到的错误如下: warning: Function components cannot be given refs. Attempts to access this ref will fa
warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
但是我在MList.js中使用React.forwardRef()。我做错了什么
ActionweekModal.js
import React,{useState,useRef}来自“React”
{ ... }
导出默认函数ActionWeekModel({navigation,route}){
const mlist=React.createRef()
{ ... }
常量滚动=()=>{
mlist.current.scrollToEnd()
}
返回(
NavigationService.goBack()}
onCancel={()=>NavigationService.goBack()}
title=“Actieweek”
>
滚动()}/>
{
如果(下一页){
fetchNextPage()
}
}}
onEndReachedThreshold={0.2}
/>
)
}
MList.js
从“React”导入React
{ ... }
const MList=React.forwardRef((props,ref)=>(
))
导出默认MList
正如a所说,您需要将ref
更改为listViewRef
,以便检索SwipeListView的引用您可以禁用{…props}
这一行并查看结果吗?@LeriGogsadze删除{…props}没有任何区别,当我这样做时,我得到类型错误:null不是一个对象(评估“Object.keys(this.props.listViewRef)”)
import React, { useState, useRef } from 'react'
{ ... }
export default function ActionWeekModal({ navigation, route }) {
const mlist = React.createRef()
{ ... }
const scroll = () => {
mlist.current.scrollToEnd()
}
return (
<MModal
isVisible={true}
onBackdropPress={() => NavigationService.goBack()}
onCancel={() => NavigationService.goBack()}
title="Actieweek"
>
<Button title={'button'} onPress={() => scroll()} />
<ScrollView>
<MList
keyExtractor={keyExtractor}
data={actionWeeks}
refreshing={isFetching}
renderItem={renderItem}
ref={mlist}
initialNumToRender={15}
onEndReached={() => {
if (hasNextPage) {
fetchNextPage()
}
}}
onEndReachedThreshold={0.2}
/>
</ScrollView>
</MModal>
)
}
import React from 'react'
{ ... }
const MList = React.forwardRef((props, ref) => (
<View style={ApplicationStyles.screen}>
<SwipeListView
style={ApplicationStyles.flatListContainer}
data={props.data}
ref={ref}
initialNumToRender={props.initialNumToRender}
keyExtractor={props.keyExtractor}
renderItem={props.renderItem}
onEndReached={props.onEndReached}
onEndReachedThreshold={props.onEndReachedThreshold ?? 1}
closeOnRowOpen
closeOnRowPress={true}
closeOnRowBeginSwipe
disableRightSwipe
{...props}
/>
</View>
))
export default MList