React自定义挂钩如何向父级返回回调函数



我有一个用于分页的自定义挂钩,我想在父类中调用SetLoadMore函数,方法是什么?

分页是返回值

handleListEnd SetFetchMore是来自父的函数

但是如何将函数从钩子传递到父

请注意:我不想在parent((中使用useEffect来检查值差异

const usePaging=(SubStories)=>{

const [FetchMore,SetFetchMore]=React.useState(false)

const [Paging,SetPaging]=React.useState(0)
const handleListEnd=()=>{
if(SubStories.length > 0 && !FetchMore){
SetFetchMore(true)

console.log("Data")
SetPaging(SubStories.length)
}
}

React.useEffect(()=>{
if(Paging > 0){
setTimeout(()=>{

//how to pass the to parent function 
SetLoadMore()
}, 300);

}
},[Paging])

return [Paging,handleListEnd,SetFetchMore,SetLoadMore]
}

这是父函数

function ParentFunction(){
const [Paging,handleListEnd,SetFetchMore,SetLoadMore] =usePaging(DataArraty)
/*
something like
const [Paging,handleListEnd,SetFetchMore,()=>{
//fire function
}] =usePaging(DataArraty)
*/
return null
}

您可以将SetLoadMore作为usePaging 的第二个参数传递

const usePaging=(SubStories, setLoadMore)=>{
// ...
React.useEffect(()=>{
if(Paging > 0){
setTimeout(()=>{
setLoadMore();
}, 300);
}
}, [Paging, setLoadMore])
}
function ParentFunction(){
const [Paging,handleListEnd,SetFetchMore] =usePaging(DataArraty, () => {
// .. do something
})
}

最新更新