GithubHelp home page GithubHelp logo

Comments (2)

Circkoooooo avatar Circkoooooo commented on April 25, 2024

It seems to be related to the new architecture of React18, but I'm not entirely sure. However, there is a workaround available.

Your code may not be the best approach to rendering two components synchronously using useRef and useState. The move event will immediately trigger the ref update, changing the style of the DOM, but setState will not.

workaround

To fix this issue, you should listen to the moveEvent, trigger setState, and then update the ref using setState."

  1. listen the moveEvent
  2. trigger setState
  3. update ref by setState
 const [{ x, y }, setCoords] = useState({ x: 0, y: 0 });
  const ref1 = useRef<HTMLDivElement>(null);

  useEffect(() => {
    document.addEventListener("mousemove", (e) => {
      setCoords({ x: e.clientX, y: e.clientY });
    });
  }, []);

  useEffect(() => {
    if (ref1.current) {
      ref1.current.style.top = y + "px";
      ref1.current.style.left = x + "px";
    }
  }, [x, y]);

from react.

blnvdanil avatar blnvdanil commented on April 25, 2024

@Circkoooooo Thanks for your answer!

I am not exactly sure, but in theory your solution sounds like "make blue square slower, so it wil be synchonous to the red square", but my initial problem (in my project) was that I was using this technic to show some floating element over canvas, the problem begins when you start dragging the canvas and updating floating coordinates, canvas drags without lag, but the floating element has noticable delay.

And this two examples was just to show that there was no difference in updating coordinates with react's useState and using dom element itself.

I tested yur solution, but that does work in my implementation Here is the link: suggested workaround
But, as I said, seems that I just made blue quare a little bit slower, the user expierence become worse :(

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.