Skip to content

Commit

Permalink
feat(react): 新增 useLocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 16, 2020
1 parent 445c0a8 commit 7a415e0
Show file tree
Hide file tree
Showing 4 changed files with 78 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,useSearchParam}.*'], f => `export * from '${f.path}'`)
// @index(['./**/*.ts', '!./**/*.{test,taro}.*', '!./{useToggle,createGlobalState,useTitle,useInterval,useSearchParam,useLocalStorage}.*'], f => `export * from '${f.path}'`)
export * from './useClassName'
export * from './useLoadMore'
export * from './useReachBottom'
Expand All @@ -28,3 +28,4 @@ export {
export { useTitle } from './useTitle'
export { useInterval, UseIntervalResult } from './useInterval'
export { useSearchParam } from './useSearchParam'
export { useLocalStorage } from './useLocalStorage'
39 changes: 39 additions & 0 deletions src/react/useLocalStorage.taro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as Taro from '@tarojs/taro'
import { act, renderHook } from '@testing-library/react-hooks'

describe('useLocalStorage', () => {
const storage: Record<any, any> = {}

beforeAll(() => {
jest.mock(
'@tarojs/taro',
() =>
({
getStorageSync: key => {
return storage[key]
},
removeStorage: payload => {
delete storage[payload.key]
},
setStorage: payload => {
storage[payload.key] = payload.data
},
} as typeof Taro),
)
})

test('表现正常', async () => {
const { useLocalStorage } = await import('./useLocalStorage.taro')
const { result: resx } = renderHook(() => useLocalStorage<string>('x'))
expect(resx.current[0]).toBe(undefined)
act(() => resx.current[1]('1'))
expect(resx.current[0]).toBe('1')
act(() => resx.current[1](prev => `${prev}_2`))
expect(resx.current[0]).toBe('1_2')
act(() => resx.current[2]())
expect(resx.current[0]).toBe(undefined)

const { result: resy } = renderHook(() => useLocalStorage('y', 5))
expect(resy.current[0]).toBe(5)
})
})
35 changes: 35 additions & 0 deletions src/react/useLocalStorage.taro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useLocalStorage as _useLocalStorage, useUpdateEffect } from 'react-use'
import { getStorageSync, removeStorage, setStorage } from '@tarojs/taro'
import { useCallback, useState } from 'react'

export const useLocalStorage: typeof _useLocalStorage = (
key,
initialValue,
// 忽略选项
_options,
) => {
const [value, setValue] = useState<ReturnType<typeof _useLocalStorage>[0]>(
() => getStorageSync(key) || initialValue,
)
useUpdateEffect(() => {
setValue(getStorageSync(key) || initialValue)
}, [key])
const set: ReturnType<typeof _useLocalStorage>[1] = useCallback(
valueOrSetter => {
if (typeof valueOrSetter === 'function') {
valueOrSetter = valueOrSetter(value)
}
setStorage({
key: key,
data: valueOrSetter,
})
setValue(valueOrSetter)
},
[key, value],
)
const remove: ReturnType<typeof _useLocalStorage>[2] = useCallback(() => {
removeStorage({ key })
setValue(undefined)
}, [key])
return [value as any, set, remove]
}
2 changes: 2 additions & 0 deletions src/react/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* istanbul ignore file */
export { useLocalStorage } from 'react-use'

0 comments on commit 7a415e0

Please sign in to comment.