Skip to content

Commit

Permalink
fix(useAdaptivityHasPointer): two pass rendering (#5666)
Browse files Browse the repository at this point in the history
Не используем двойной рендер если
значение было проброшено через контекст
  • Loading branch information
SevereCloud authored Aug 21, 2023
1 parent a3906e5 commit 058e705
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
29 changes: 29 additions & 0 deletions packages/vkui/src/hooks/useAdaptivityHasPointer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { AdaptivityProvider } from '../components/AdaptivityProvider/AdaptivityProvider';
import { useAdaptivityHasPointer } from './useAdaptivityHasPointer';

describe(useAdaptivityHasPointer, () => {
it('returns on client', () => {
const { result } = renderHook(useAdaptivityHasPointer, {});
expect(result.current).toEqual(true);
});

it('context hasPointer={true}', () => {
const { result } = renderHook(useAdaptivityHasPointer, {
wrapper: ({ children }) => (
<AdaptivityProvider hasPointer={true}>{children}</AdaptivityProvider>
),
});
expect(result.current).toEqual(true);
});

it('context hasPointer={false}', () => {
const { result } = renderHook(useAdaptivityHasPointer, {
wrapper: ({ children }) => (
<AdaptivityProvider hasPointer={false}>{children}</AdaptivityProvider>
),
});
expect(result.current).toEqual(false);
});
});
11 changes: 6 additions & 5 deletions packages/vkui/src/hooks/useAdaptivityHasPointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export function useAdaptivityHasPointer(deferDetect?: true): undefined | boolean
export function useAdaptivityHasPointer(deferDetect?: false): boolean;
export function useAdaptivityHasPointer(deferDetect = true): undefined | boolean {
const { hasPointer: hasPointerContext } = React.useContext(AdaptivityContext);
const hasPointer = hasPointerContext === undefined ? hasPointerLib : hasPointerContext;

const isClient = useIsClient(!deferDetect);
if (!isClient) {
return undefined;
const needTwoPassRendering = !deferDetect || hasPointerContext !== undefined;

const isClient = useIsClient(needTwoPassRendering);
if (!isClient || hasPointerContext !== undefined) {
return hasPointerContext;
}

return hasPointer;
return hasPointerLib;
}

0 comments on commit 058e705

Please sign in to comment.