【问题标题】:ReactJS useGesture: typeerror set is not a functionReactJS useGesture:类型错误集不是函数
【发布时间】:2021-04-02 17:31:53
【问题描述】:

我一直在尝试从 useGesture documentation 获取示例工作

但无论我尝试什么,我都会收到 typeerror set is not a function 错误

import './App.css';
import React, {useState} from "react"
import { useSpring, animated } from "react-spring"
import { useDrag } from "react-use-gesture"
import data from './data';

function App() {
  const [{ x, y }, set] = useSpring(() => ({ x: 0, y: 0 }))

  // Set the drag hook and define component movement based on gesture data
  const bind = useDrag(({ down, movement: [mx, my] }) => {
    set({ x: down ? mx : 0, y: down ? my : 0 })
  })

  // Bind it to a component
  return <animated.div {...bind()} style={{ x, y }} className="box"/>
}

export default App;

我的版本是最新的

{
  "name": "gesture-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.10",
    "@testing-library/react": "^11.2.6",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "react-spring": "^9.0.0",
    "react-use-gesture": "^9.1.3",
    "web-vitals": "^1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我尝试了许多不同的版本,但总是收到此错误。有什么想法吗?

【问题讨论】:

    标签: reactjs react-spring


    【解决方案1】:

    我今天也遇到了这个问题。显然 useSpring now 的第二个返回值(就像今天安装的那样)返回“弹簧对象”。试试

    function App() {
        const [{ x, y }, spring] = useSpring(() => ({ x: 0, y: 0 }));
    
        // Set the drag hook and define component movement based on gesture data
        const bind = useDrag(({ down, movement: [mx, my] }) => {
            spring.set({ x: down ? mx : 0, y: down ? my : 0 });
        });
    
        // Bind it to a component
        return <animated.div {...bind()} style={{ x, y }} className="box" />;
    }
    

    依赖是

    "react-spring": "^9.0.0",
    "react-use-gesture": "^9.1.3",
    

    这可能没有太大帮助,随着向 9.0.0 的过渡,还有更多尚未在文档中找到的更改。我的建议是转到examples 并检查沙箱以获取有效的版本组合。

    【讨论】:

    • 非常感谢,我把头撞在墙上试图弄清楚。很遗憾他们没有在文档中得到这个。非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 2018-01-02
    • 2021-12-01
    • 2021-09-14
    • 2016-06-05
    • 2016-02-26
    • 2013-04-01
    • 2016-03-06
    相关资源
    最近更新 更多