简体   繁体   English

在 React 功能组件中使用 useRef 时出现无法读取 null 错误的属性“样式”

[英]Getting a Cannot read property 'style' of null Error When Using useRef Within a React Functional Component

I am trying to re-create this circle-follow-cursor effect found here but within a React functional component.我正在尝试重新创建这里找到的圆形跟随光标效果但在 React 功能组件中。

const Button = props => {
  const ballRef = React.useRef(null)

  let mouseX = 0
  let mouseY = 0
  let ballX = 0
  let ballY = 0
  let speed = 0.2

  function animate() {
    let distX = mouseX - ballX
    let distY = mouseY - ballY
    ballX = ballX + distX * speed
    ballY = ballY + distY * speed
    ballRef.current.style.left = ballX + "px"
    ballRef.current.style.top = ballY + "px"

    requestAnimationFrame(animate)
  }

  animate()

  document.addEventListener("mousemove", function(event) {
    mouseX = event.pageX
    mouseY = event.pageY
  })

  return (
      <div ref={ballRef} >Button</div>
  )
}

export default Button

The thing is, the code works the very first time I implemented it, then when I refresh the page it throws this error:问题是,代码在我第一次实现它时就起作用了,然后当我刷新页面时它会抛出这个错误:

TypeError: Cannot read property 'style' of null
animate
src/components/button/index.js:19
  16 | let distY = mouseY - ballY
  17 | ballX = ballX + distX * speed
  18 | ballY = ballY + distY * speed
> 19 | ballRef.current.style.left = ballX + "px"
  20 | ballRef.current.style.top = ballY + "px"
  21 | 
  22 | requestAnimationFrame(animate)

I'm sure it's related to how React re-renders the UI based on changes but I thought the useRef hook helps with that.我确定这与 React 如何根据更改重新呈现 UI 相关,但我认为 useRef 挂钩对此有所帮助。 Anyway, I thought I'd get some fresh eyes on this.无论如何,我想我会对此有所了解。

You need to set animate() in useEffect您需要在useEffect设置animate()

useEffect(() => {
    animate();
}, []);

You're voilating several rules while using React Component and hooks, here's a much standard version of your code:您在使用 React Component 和 hooks 时违反了几条规则,这是您的代码的标准版本:

// Put the variables outside to make sure they won't reset while component refresh
let mouseX = 0
let mouseY = 0
let ballX = 0
let ballY = 0
let speed = 0.2

const Button = props => {
    const ballRef = React.useRef(null);

    // use useCallback to make sure this function won't create multiple copies when refreshed
    const animate = React.useCallback(() => {
        // Make sure the ballRef is created, at the first run, the ball ref is still null
        if(ballRef && ballRef.current) {
            let distX = mouseX - ballX
            let distY = mouseY - ballY
            ballX = ballX + distX * speed
            ballY = ballY + distY * speed
            ballRef.current.style.left = ballX + "px"
            ballRef.current.style.top = ballY + "px"
        }

        requestAnimationFrame(animate)
    }, [ballRef, mouseX, mouseY, ballX, ballY]);

    // This make sure the function inside only runs when the component is mounted
    React.useEffect(()=>{
        const onMouseMove = event => {
            mouseX = event.pageX;
            mouseY = event.pageY;
        };

        document.addEventListener("mousemove", onMouseMove);

        animate();

        // When the component is unmount, remove the listener
        return () => document.removeEventListener("mousemove", onMouseMove);
    }, []);

    return (
        <div ref={ballRef} >Button</div>
    )
}

export default Button

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React useRef 在路由更改时返回无法读取 null 的属性“样式”的错误 - React useRef returns error of Cannot read property 'style' of null on route change 使用 useeffect 和 useref TypeError 时出错:无法读取 null 的属性“getBoundingClientRect” - Error using useeffect and useref TypeError: Cannot read property 'getBoundingClientRect' of null React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") while using React Router Dom - React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") while using React Router Dom TypeError: 使用带有功能组件的 redux 时无法读取属性 - TypeError: Cannot read property when using redux with functional component 为什么会出现“无法读取null的属性样式”错误? - Why I am getting “cannot read property style of null” error? 不断收到“无法读取 null 的属性样式”错误 - Keep getting “cannot read property style of null” error 无法读取空反应的属性“样式” - Cannot read property 'style' of null React 无法在 React 中读取 null 的属性“样式” - Cannot read property 'style' of null in React 使用NativeBase Toast组件时,无法在React Native中读取null的属性&#39;_root&#39; - Cannot read property '_root' of null in React Native when using NativeBase Toast component 使用反应挂钩 (useRef) 将 class 组件转换为功能组件—如何避免“未定义”错误 - Converting class component to functional component using react hooks (useRef)—how to avoid “undefined” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM