【问题标题】:Slow performance react-use-gesture and react-spring性能缓慢的反应使用手势和反应弹簧
【发布时间】:2021-05-25 15:53:38
【问题描述】:

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

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

信息:

  • React Use Gesture 版本:(我尝试了各种版本的组合都没有影响,但这是代码框中的配置)
    "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"
  • 设备:iPhone 12 pro max
  • 操作系统:[例如iOS14.5.1]
  • 浏览器铬
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>
  );
}

【问题讨论】:

    标签: javascript reactjs draggable react-spring react-use-gesture


    【解决方案1】:

    我梳理了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>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2019-09-18
      • 2018-07-03
      • 2021-08-12
      • 2021-05-09
      • 2020-09-19
      相关资源
      最近更新 更多