性能缓慢 react-use-gesture 和 react-spring

Slow performance react-use-gesture and react-spring

this one 等文档中的拖动示例非常快,并且非常准确地映射到我的手指位置。我试过一对一地复制示例,但要赶上我的手指位置有明显的滞后。

Here is a code sandbox example 在真实设备上测试时有明显的延迟。

信息:

    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-scripts": "4.0.0",
    "react-spring": "9.1.2",
    "react-use-gesture": "9.1.3"
function PullRelease() {
  const [{ x }, api] = useSpring(() => ({ x: 0 }));
  const bind = useDrag(({ movement: [mx], down }) =>
    api.start({ x: down ? mx : 0 })
  );
  return (
    <animated.div {...bind()} style={{ padding: 40, background: "gold", x }}>
      Hello World
    </animated.div>
  );
}

我梳理了source code of the examples。问题实际上是 react-spring,我需要在触摸处于活动状态时提供 immediate: true

https://codesandbox.io/s/react-spring-gesture-basic-swipe-immediate-6bje9?file=/src/App.js

function PullRelease() {
  const [{ x }, api] = useSpring(() => ({ x: 0 }));
  const bind = useDrag(({ movement: [mx], down }) =>
    api.start({ x: down ? mx : 0, 
        immediate: down // the key line
    })
  );
  return (
    <animated.div {...bind()} style={{ padding: 40, background: "gold", x }}>
      Hello World
    </animated.div>
  );
}