Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No double click detection with useEffect #24

Open
carlosporta opened this issue Nov 24, 2021 · 4 comments
Open

No double click detection with useEffect #24

carlosporta opened this issue Nov 24, 2021 · 4 comments

Comments

@carlosporta
Copy link

I am trying to detect double clicks and single clicks on buttons.

function App() {
  const buttonRef = useRef();
  const [message, setMessage] = useState();
  const [ws, setWS] = useState();

  useEffect(() => {
   const localWS = new WebSocket('ws://192.168.31.104:8000/ws');
    localWS.onmessage = (event) => {}
  }, []);

  useDoubleClick({
    onSingleClick: e => {
      console.log(e, 'single click');
    },
    onDoubleClick: e => {
      console.log(e, 'double click');
    },
    ref: buttonRef,
    latency: 250
  });

  return <button ref={buttonRef}>Click Me</button>
}
export default App;

This function works perfectly, but when I try to change a state variable, the useDoubleClick hooks stops to detect double clicks.

function App() {
  const buttonRef = useRef();
  const [message, setMessage] = useState();
  const [ws, setWS] = useState();

  useEffect(() => {
    const localWS = new WebSocket('ws://192.168.31.104:8000/ws');
    localWS.onmessage = (event) => {
      setMessage(JSON.parse(event.data)) // HERE
      setWS(localWS); // HERE
    }
  }, []);
  
  useDoubleClick({
    onSingleClick: e => {
      console.log(e, 'single click');
    },
    onDoubleClick: e => {
      console.log(e, 'double click');
    },
    ref: buttonRef,
    latency: 250
  });
  
  return <button ref={buttonRef}>Click Me</button>

}

What am I doing wrong?

@vedro-compota
Copy link

can you create small demo app to check this problem?

@episode17
Copy link

episode17 commented Apr 11, 2024

Late but I was exploring this recently: The hook has no dependency array in it's useEffect so your setState is resetting the internal click count, resulting in only single clicks being registered.

@tim-soft
Copy link
Owner

Does this sandbox work for you?

https://codesandbox.io/s/use-double-click-f7e33?expanddevtools=1&fontsize=14

Feel free to provide a repro of it not working

@TomerHir
Copy link

TomerHir commented Apr 22, 2024

this pr will solve your issue once it's verified as production-ready:
#32

additionally, you could THEORETICALLY add this component to your code and use it instead of directly calling useDoubleClick but... better wait for the PR to pass

/**
 * component that handles a rerender in the middle of the double click event.
 * yes.
 * seriously.
 */
const DoubleClickComponent = memo(function DoubleClickComponent<TClickableHTML extends HTMLElement>({
	onSingleClick,
	onDoubleClick,
	clickableRef,
	latency,
}: {
	onSingleClick: (e: MouseEvent) => void;
	onDoubleClick: (e: MouseEvent) => void;
	clickableRef: MutableRefObject<TClickableHTML>;
	latency?: number;
}) {
	useDoubleClick({
		onSingleClick,
		onDoubleClick,
		ref: clickableRef,
		latency: latency,
	});
	return null;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants