-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add new useDebounce() in /next that returns [val, isPending] (#…
- Loading branch information
Showing
13 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
'@data-client/react': patch | ||
--- | ||
|
||
New [useDebounce()](https://dataclient.io/docs/api/useDebounce) in /next that integrates useTransition() | ||
|
||
```ts | ||
import { useDebounce } from '@data-client/react/next'; | ||
const [debouncedQuery, isPending] = useDebounce(query, 100); | ||
``` | ||
|
||
- Returns tuple - to include isPending | ||
- Any Suspense triggered due to value change will continue showing | ||
the previous contents until it is finished loading. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { Endpoint } from '@data-client/endpoint'; | ||
import { NetworkError } from '@data-client/rest'; | ||
import { renderHook, act } from '@data-client/test'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
import React, { ReactElement, StrictMode, useTransition } from 'react'; | ||
|
||
import AsyncBoundary from '../../components/AsyncBoundary'; | ||
import DataProvider from '../../components/DataProvider'; | ||
import { useSuspense } from '../../hooks'; | ||
import useDebounce from '../useDebounce'; | ||
|
||
describe('useDebounce()', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should not update until delay has passed', () => { | ||
const { result, rerender } = renderHook( | ||
({ value }: { value: string }) => { | ||
return useDebounce(value, 100); | ||
}, | ||
{ initialProps: { value: 'initial' } }, | ||
); | ||
expect(result.current).toEqual(['initial', false]); | ||
jest.advanceTimersByTime(10); | ||
rerender({ value: 'next' }); | ||
rerender({ value: 'third' }); | ||
expect(result.current).toEqual(['initial', false]); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
expect(result.current).toEqual(['third', false]); | ||
}); | ||
|
||
it('should never update when updatable is false', () => { | ||
const { result, rerender } = renderHook( | ||
({ value, updatable }: { value: string; updatable: boolean }) => { | ||
return useDebounce(value, 100, updatable); | ||
}, | ||
{ initialProps: { value: 'initial', updatable: false } }, | ||
); | ||
expect(result.current).toEqual(['initial', false]); | ||
jest.advanceTimersByTime(10); | ||
rerender({ value: 'next', updatable: false }); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
expect(result.current).toEqual(['initial', false]); | ||
rerender({ value: 'third', updatable: true }); | ||
expect(result.current).toEqual(['initial', false]); | ||
jest.advanceTimersByTime(10); | ||
expect(result.current).toEqual(['initial', false]); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
expect(result.current).toEqual(['third', false]); | ||
}); | ||
|
||
it('should be pending while async operation is performed based on debounced value', async () => { | ||
const issueQuery = new Endpoint( | ||
({ q }: { q: string }) => Promise.resolve({ q, text: 'hi' }), | ||
{ name: 'issueQuery' }, | ||
); | ||
function IssueList({ q }: { q: string }) { | ||
const response = useSuspense(issueQuery, { q }); | ||
return <div>{response.q}</div>; | ||
} | ||
function Search({ query }: { query: string }) { | ||
const [debouncedQuery, isPending] = useDebounce(query, 100); | ||
return ( | ||
<div> | ||
{isPending ? | ||
<span>loading</span> | ||
: null} | ||
<AsyncBoundary fallback={<div>searching...</div>}> | ||
<IssueList q={debouncedQuery} /> | ||
</AsyncBoundary> | ||
</div> | ||
); | ||
} | ||
|
||
const tree = ( | ||
<DataProvider> | ||
<Search query="initial" /> | ||
</DataProvider> | ||
); | ||
const { queryByText, rerender, getByText } = render(tree); | ||
expect(queryByText(/loading/i)).toBeNull(); | ||
expect(getByText(/searching/i)).not.toBeNull(); | ||
|
||
await waitFor(() => expect(queryByText(/searching/i)).toBeNull()); | ||
expect(getByText(/initial/i)).not.toBeNull(); | ||
rerender( | ||
<DataProvider> | ||
<Search query="second" /> | ||
</DataProvider>, | ||
); | ||
rerender( | ||
<DataProvider> | ||
<Search query="third" /> | ||
</DataProvider>, | ||
); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
// only check in react 18 | ||
if ('useTransition' in React) { | ||
// isPending | ||
expect(getByText(/loading/i)).not.toBeNull(); | ||
} | ||
// keep showing previous values | ||
expect(getByText(/initial/i)).not.toBeNull(); | ||
// only check in react 18 | ||
if ('useTransition' in React) { | ||
expect(queryByText(/searching/i)).toBeNull(); | ||
} | ||
|
||
await waitFor(() => expect(queryByText(/loading/i)).toBeNull()); | ||
expect(getByText(/third/i)).not.toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useDebounce } from './useDebounce.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useEffect, useState, useTransition } from 'react'; | ||
|
||
/** | ||
* Keeps value updated after delay time | ||
* | ||
* @see https://dataclient.io/docs/api/useDebounce | ||
* @param value Any immutable value | ||
* @param delay Time in miliseconds to wait til updating the value | ||
* @param updatable Whether to update at all | ||
* @example | ||
``` | ||
const [debouncedQuery, isPending] = useDebounce(query, 200); | ||
const list = useSuspense(getThings, { query: debouncedQuery }); | ||
``` | ||
*/ | ||
export default function useDebounce<T>( | ||
value: T, | ||
delay: number, | ||
updatable = true, | ||
): [T, boolean] { | ||
const [debouncedValue, setDebouncedValue] = useState(value); | ||
const [isPending, startTransition] = useTran(); | ||
|
||
useEffect(() => { | ||
if (!updatable) return; | ||
|
||
const handler = setTimeout(() => { | ||
startTransition(() => setDebouncedValue(value)); | ||
}, delay); | ||
return () => { | ||
clearTimeout(handler); | ||
}; | ||
}, [value, delay, updatable]); | ||
|
||
return [debouncedValue, isPending]; | ||
} | ||
|
||
// compatibility with older react versions | ||
const useTran = useTransition ?? (() => [false, identityRun]); | ||
const identityRun = (fun: (...args: any) => any) => fun(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletion
16
website/src/components/Playground/editor-types/@data-client/react/next.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,16 @@ | ||
/** | ||
* Keeps value updated after delay time | ||
* | ||
* @see https://dataclient.io/docs/api/useDebounce | ||
* @param value Any immutable value | ||
* @param delay Time in miliseconds to wait til updating the value | ||
* @param updatable Whether to update at all | ||
* @example | ||
``` | ||
const [debouncedQuery, isPending] = useDebounce(query, 200); | ||
const list = useSuspense(getThings, { query: debouncedQuery }); | ||
``` | ||
*/ | ||
declare function useDebounce<T>(value: T, delay: number, updatable?: boolean): [T, boolean]; | ||
|
||
export { } | ||
export { useDebounce }; |