beautiful-react-hooks
Version:
A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development
21 lines (20 loc) • 710 B
JavaScript
import { useState } from 'react';
import useTouchEvents from './useTouchEvents';
/**
* Returns the current touches from the touch move event.
* It possibly accepts a DOM ref representing the mouse target.
* If a target is not provided the state will be caught globally.
*/
const useTouchState = (targetRef) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const [state, setState] = useState({ length: 0 });
const { onTouchStart, onTouchMove } = useTouchEvents(targetRef);
onTouchStart((event) => {
setState(event.touches);
});
onTouchMove((event) => {
setState(event.touches);
});
return state;
};
export default useTouchState;