Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check canUseTouchScreen on each render #24356

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/components/Attachments/AttachmentCarousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Navigation from '../../../libs/Navigation/Navigation';
import BlockingView from '../../BlockingViews/BlockingView';
import * as Illustrations from '../../Icon/Illustrations';
import variables from '../../../styles/variables';
import useCanUseTouchScreen from '../../../hooks/useCanUseTouchScreen';

const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
const viewabilityConfig = {
// To facilitate paging through the attachments, we want to consider an item "viewable" when it is
// more than 95% visible. When that happens we update the page index in the state.
Expand All @@ -31,6 +31,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
const scrollRef = useRef(null);

const {windowWidth, isSmallScreenWidth} = useWindowDimensions();
const canUseTouchScreen = useCanUseTouchScreen();

const [containerWidth, setContainerWidth] = useState(0);
const [page, setPage] = useState(0);
Expand Down Expand Up @@ -99,7 +100,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl

scrollRef.current.scrollToIndex({index: nextIndex, animated: canUseTouchScreen});
},
[attachments, page],
[attachments, canUseTouchScreen, page],
);

/**
Expand Down Expand Up @@ -160,7 +161,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
isUsedInCarousel
/>
),
[activeSource, setShouldShowArrows, shouldShowArrows],
[activeSource, canUseTouchScreen, setShouldShowArrows, shouldShowArrows],
);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {useCallback, useEffect, useRef, useState} from 'react';
import CONST from '../../../CONST';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';

const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
import useCanUseTouchScreen from '../../../hooks/useCanUseTouchScreen';

function useCarouselArrows() {
const canUseTouchScreen = useCanUseTouchScreen();
const [shouldShowArrows, setShouldShowArrowsInternal] = useState(canUseTouchScreen);
const autoHideArrowTimeout = useRef(null);

Expand All @@ -25,7 +25,7 @@ function useCarouselArrows() {
autoHideArrowTimeout.current = setTimeout(() => {
setShouldShowArrowsInternal(false);
}, CONST.ARROW_HIDE_DELAY);
}, [cancelAutoHideArrows]);
}, [canUseTouchScreen, cancelAutoHideArrows]);

const setShouldShowArrows = useCallback(
(show = true) => {
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useCanUseTouchScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {useEffect, useState} from 'react';
import * as DeviceCapabilities from '../libs/DeviceCapabilities';

/**
* This hooks returns a boolean of whether the device is a touch screen or not.
chrispader marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Boolean}
*/
export default function useCanUseTouchScreen() {
const [canUseTouchScreen, setCanUseTouchScreen] = useState(DeviceCapabilities.canUseTouchScreen());

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
const newCanUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
if (canUseTouchScreen === newCanUseTouchScreen) return;
setCanUseTouchScreen();
chrispader marked this conversation as resolved.
Show resolved Hide resolved
});

return canUseTouchScreen;
Copy link
Contributor

@kidroca kidroca Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can achieve the same outcome with a more straightforward approach:

Suggested change
const [canUseTouchScreen, setCanUseTouchScreen] = useState(DeviceCapabilities.canUseTouchScreen());
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
const newCanUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
if (canUseTouchScreen === newCanUseTouchScreen) return;
setCanUseTouchScreen();
});
return canUseTouchScreen;
return DeviceCapabilities.canUseTouchScreen();

Here's why this alternative is equivalent:

  1. The current implementation uses a useEffect that runs logic on each render (since there's no dependencies array).
  2. In such a scenario, you could simply execute the logic in the hook body, eliminating the need for a useEffect and state

}