This commit is contained in:
imeepos
2026-01-28 18:46:44 +08:00
parent e478b126cd
commit 7c50d396e9
40 changed files with 6567 additions and 39 deletions

View File

@@ -0,0 +1,105 @@
/**
* useUserFavorites Hook 使用示例
*
* 这个示例展示了如何使用 useUserFavorites Hook 来获取用户的收藏列表
*/
import { useUserFavorites } from '@/hooks'
// 示例1基本用法
function MyFavoritesList() {
const {
favorites,
loading,
error,
execute,
refetch,
loadMore,
hasMore,
} = useUserFavorites()
useEffect(() => {
// 初始加载
execute()
}, [])
if (loading && !favorites.length) {
return <LoadingSpinner />
}
if (error) {
return <ErrorMessage message={error.message} />
}
return (
<FlatList
data={favorites}
renderItem={({ item }) => (
<FavoriteItem favorite={item} />
)}
onEndReached={() => {
if (hasMore && !loading) {
loadMore()
}
}}
onEndReachedThreshold={0.5}
ListFooterComponent={() => loading ? <LoadingMoreSpinner /> : null}
/>
)
}
// 示例2带自定义参数
function MyFavoritesWithParams() {
const { favorites, loading } = useUserFavorites({
limit: 50, // 每页50条
})
// ...
}
// 示例3带搜索功能
function SearchableFavorites() {
const [searchQuery, setSearchQuery] = useState('')
const { favorites, execute, loading } = useUserFavorites()
const handleSearch = (query: string) => {
setSearchQuery(query)
execute({ search: query })
}
// ...
}
// 示例4下拉刷新
function RefreshableFavorites() {
const { favorites, loading, refreshing, refetch } = useUserFavorites()
const handleRefresh = () => {
refetch()
}
return (
<FlatList
data={favorites}
refreshing={loading}
onRefresh={handleRefresh}
// ...
/>
)
}
// 示例5使用返回的完整数据
function FavoritesWithStats() {
const { data, favorites } = useUserFavorites()
return (
<View>
<Text>Total Favorites: {data?.total || 0}</Text>
<Text>Current Page: {data?.page || 1}</Text>
<Text>Per Page: {data?.limit || 20}</Text>
{/* 列表... */}
</View>
)
}
export type { UserFavoriteItem } from '@/hooks'