Comments (2)
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."
- listen the
moveEvent
- trigger
setState
- update
ref
bysetState
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.
@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)
- @l0gicgate HOT 1
- [DevTools Bug] Cannot remove node "0" because no matching node was found in the Store. HOT 1
- Bug: SSR: "Text content did not match" for inline CSS in style element HOT 3
- Bug: useContext not updating render when a getter is used as context HOT 2
- Bug: ref trigger useEffect execute HOT 10
- Bug: Internal React error: Expected static flag was missing. Please notify the React team HOT 2
- Bug: useContext
- Bug: uSES with synchronous updating a component causes a warning HOT 6
- Bug: StrictMode not work for `reactive` data trigger a update
- Bug: useState cut out to Component in iOS behaves strangely.
- Bug: form data is lost upon hydration HOT 3
- Bug: Uncaught Error: ReactDOM was loaded before React. Make sure you load the React package before loading ReactDOM. HOT 1
- Bug: All child components will rerender if providers not rendering children from props HOT 2
- Bug: react-dom.development.js:16572 Uncaught Error: Should have a queue. This is likely a bug in React. Please file an issue.
- Bug: [Firefox] Removing an element with contentEditable="true" within an iframe and an RTL selection causes an exception
- [eslint-plugin-react-hooks] (react-hooks/exhaustive-deps) allow configuring a prop to throw errors with unnecessary dependencies in useEffects HOT 7
- Let's See This Project about GodJobForReact-Vercel HOT 3
- Bug: HOT 5
- Provide a method to create synthetic events HOT 2
- Bug: Resetting the value comes from dynamic `defaultValue` of `<input type="search" />` won't trigger `onChange` HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.