Using useRef instead of useState in <Handler/> (no need to re-render when mouse state is updated).
This commit is contained in:
parent
d758775ab7
commit
4f64d9906c
|
@ -1,4 +1,4 @@
|
||||||
import react, { useState } from 'react';
|
import react, { useRef, useState } from 'react';
|
||||||
import { Point } from './types';
|
import { Point } from './types';
|
||||||
import './Handler.css';
|
import './Handler.css';
|
||||||
import { handlersConfig } from './config';
|
import { handlersConfig } from './config';
|
||||||
|
@ -46,50 +46,51 @@ export const Handlers: react.FC<HandlersProperties> = (
|
||||||
timestamp: 0,
|
timestamp: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
const [mouseState, setMouseState] = useState(initialMouseState);
|
const mouseState = useRef(initialMouseState);
|
||||||
|
|
||||||
const mouseLeaveHandler = (event: any) => {
|
const mouseLeaveHandler = (event: any) => {
|
||||||
genericHandler(event);
|
genericHandler(event);
|
||||||
setMouseState(initialMouseState);
|
mouseState.current = initialMouseState;
|
||||||
};
|
};
|
||||||
|
|
||||||
const mouseDownHandler = (event: any) => {
|
const mouseDownHandler = (event: any) => {
|
||||||
genericHandler(event);
|
genericHandler(event);
|
||||||
setMouseState({
|
mouseState.current = {
|
||||||
down: true,
|
down: true,
|
||||||
starting: { x: event.clientX, y: event.clientY },
|
starting: { x: event.clientX, y: event.clientY },
|
||||||
timestamp: 0,
|
timestamp: 0,
|
||||||
});
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const mouseUpHandler = (event: any) => {
|
const mouseUpHandler = (event: any) => {
|
||||||
genericHandler(event);
|
genericHandler(event);
|
||||||
setMouseState(initialMouseState);
|
mouseState.current = initialMouseState;
|
||||||
};
|
};
|
||||||
|
|
||||||
const mouseMoveHandler = (event: any) => {
|
const mouseMoveHandler = (event: any) => {
|
||||||
if (
|
if (
|
||||||
mouseState.down &&
|
mouseState.current.down &&
|
||||||
Date.now() - mouseState.timestamp > handlersConfig.mouseMoveThrottleDelay
|
Date.now() - mouseState.current.timestamp >
|
||||||
|
handlersConfig.mouseMoveThrottleDelay
|
||||||
) {
|
) {
|
||||||
genericHandler(event);
|
genericHandler(event);
|
||||||
if (mouseState.down) {
|
if (mouseState.current.down) {
|
||||||
props.transformMap(
|
props.transformMap(
|
||||||
{
|
{
|
||||||
x: event.clientX - mouseState.starting.x,
|
x: event.clientX - mouseState.current.starting.x,
|
||||||
y: event.clientY - mouseState.starting.y,
|
y: event.clientY - mouseState.current.starting.y,
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
setMouseState({
|
mouseState.current = {
|
||||||
down: true,
|
down: true,
|
||||||
starting: {
|
starting: {
|
||||||
x: event.clientX,
|
x: event.clientX,
|
||||||
y: event.clientY,
|
y: event.clientY,
|
||||||
},
|
},
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue