Skip to content

Commit

Permalink
feat(react): 新增 useReachBottom.taro
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 24, 2020
1 parent 1d304d6 commit 9afc481
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/react/useReachBottom.taro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Taro from '@tarojs/taro'
import { AnyFunction } from '../types'
import { renderHook } from '@testing-library/react-hooks'

describe('useReachBottom.taro', () => {
let useReachBottomCb: AnyFunction
const useReachBottom: AnyFunction = jest
.fn()
.mockImplementation((cb: AnyFunction) => {
useReachBottomCb = cb
})

beforeAll(() => {
jest.mock(
'@tarojs/taro',
() =>
({
useReachBottom: useReachBottom,
} as typeof Taro),
)
})

test('表现正常', async () => {
const { useReachBottom } = await import('./useReachBottom.taro')
const cb = jest.fn()
renderHook(() => useReachBottom(cb))
// 初始立即触发一次
expect(cb).toBeCalled().toBeCalledTimes(1)
expect(useReachBottomCb).toBeFunction().toBe(cb)
})
})
19 changes: 19 additions & 0 deletions src/react/useReachBottom.taro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useReachBottom as _useReachBottom } from './useReachBottom'
import { useEffect, useRef } from 'react'
import { useReachBottom as useTaroReachBottom } from '@tarojs/taro'

export const useReachBottom: typeof _useReachBottom = (
callback,
// 小程序下该项在页面配置中的 onReachBottomDistance 设置
_offset,
) => {
const ref = useRef<any>()

// 立即触发一次回调
useEffect(callback, [])

useTaroReachBottom(callback)

// 小程序下始终相对于窗口,ref 无实际作用,仅起兼容作用
return ref
}

0 comments on commit 9afc481

Please sign in to comment.