I've created a simple draggable component. I'm using the react-swipeable library but my question is about any event which repeatedly and in quick succession updates useState.
With the console.log in onSwiping I can see that the function is called many times per second, which is what I need to update the position of the component. This works however is there a performance issue with calling setSwipeX so frequently?
The entire component will re-render very fast as it's dragged. Is this OK or would it be better to just update swipeX without re-rendering the entire component, and is this even possible?
import React from "react";
import { useSwipeable } from "react-swipeable";
export default function App() {
const [swipeX, setSwipeX] = React.useState(0);
const swipeHandlers = useSwipeable({
onSwiping: (e) => {
console.log(e);
setSwipeX(-e.deltaX);
},
trackMouse: true
});
return (
<div
className="App"
{...swipeHandlers}
style={{
transform: `translateX(${swipeX}px)`
}}
>
<h1>Stuff</h1>
</div>
);
}
https://codesandbox.io/s/relaxed-minsky-ni7n9?file=/src/App.js