Skip to content

Commit

Permalink
feat(react): 新增 useSearchParam
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 9, 2020
1 parent 38f84ed commit 119a864
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

export * from 'react-use'

// @index(['./**/*.ts', '!./**/*.{test,taro}.*', '!./{useToggle,createGlobalState,useTitle,useInterval}.*'], f => `export * from '${f.path}'`)
// @index(['./**/*.ts', '!./**/*.{test,taro}.*', '!./{useToggle,createGlobalState,useTitle,useInterval,useSearchParam}.*'], f => `export * from '${f.path}'`)
export * from './useClassName'
export * from './useLoadMore'
export * from './useReachBottom'
Expand All @@ -27,3 +27,4 @@ export {
} from './createGlobalState'
export { useTitle } from './useTitle'
export { useInterval, UseIntervalResult } from './useInterval'
export { useSearchParam } from './useSearchParam'
32 changes: 32 additions & 0 deletions src/react/useSearchParam.taro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as Taro from '@tarojs/taro'
import { AnyFunction } from '../types'
import { renderHook } from '@testing-library/react-hooks'

describe('useSearchParam.taro', () => {
const useRouter: AnyFunction = jest.fn().mockImplementation(() => {
return {
params: {
id: '789',
},
}
})

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

test('表现正常', async () => {
const { useSearchParam } = await import('./useSearchParam.taro')
const { result } = renderHook(() => {
return useSearchParam('id')
})

expect(result.current).toBe('789')
})
})
7 changes: 7 additions & 0 deletions src/react/useSearchParam.taro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useSearchParam as _useSearchParam } from 'react-use'
import { useRouter } from '@tarojs/taro'

export const useSearchParam: typeof _useSearchParam = param => {
const { params } = useRouter()
return params[param] == null ? null : params[param]
}
2 changes: 2 additions & 0 deletions src/react/useSearchParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* istanbul ignore file */
export { useSearchParam } from 'react-use'

0 comments on commit 119a864

Please sign in to comment.