question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onClick not invoked if `onMouseDown` is present and updates state

See original GitHub issue

Let me start by saying that this is a very weird bug. I tried investigating your source code and I could not find anything that would possible lead to this.

It also happens if ouMouseUp, onMouseOver are provided.

Version

2.1.1

This bug happens with or without the babel plugin.

Reproduction

https://codesandbox.io/s/mZz51wGPG

Steps to reproduce

Just add a onMouseDown and onClick together where the onMouseDown update the react state.

const TestDiv = styled.div``;
 <TestDiv onMouseDown={() => this.setState({ hi: true })} onClick={() => alert('hi')}>click me, it does not work</TestDiv>

Expected Behavior

onClick is called

Actual Behavior

onClick is not called

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
alansouzaticommented, Jul 25, 2017

FYI. In my case I was doing stuff like this:

import StyledButton from './StyledButton';

const Button = ({ href, ...rest }) => {
  let Tag = StyledButton;
  if (href) {
     Tag = StyledButton.withComponent('a');
  }
  return (
    <Tag {...rest} href={href} />
  );
};

Based on your findings and the documentation, I replaced it to be something like this (which makes totally sense):

import StyledButton from './StyledButton';

const AnchorStyledButton = StyledButton.withComponent('a');

const Button = ({ href, ...rest }) => {
  const Tag = href ? AnchorStyledButton : StyledButton;
  return (
    <Tag {...rest} href={href} />
  );
};

I’m sharing here just in case hits a similar problem 👍

1reaction
alansouzaticommented, Jul 25, 2017

nicely done. thanks for the quick follow up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

onMouseDown event in react does not trigger state changes ...
I made a simple demo here and it seems to work fine with both onClick and onMouseOver . Click the text and check...
Read more >
onMouseDown event in react does not trigger state changes ...
Coding example for the question onMouseDown event in react does not trigger state changes but onClick does-Reactjs.
Read more >
Mouse events - The Modern JavaScript Tutorial
Mouse event types. We've already seen some of these events: mousedown/mouseup: Mouse button is clicked/released over an element.
Read more >
ASP.NET Core Blazor event handling - Microsoft Learn
Learn about Blazor's event handling features, including event argument types, event callbacks, and managing default browser events.
Read more >
React: Event Bubbling and Capturing - Robin Wieruch
mousedown ; mouseup; click. This particular event list is displayed in its order of execution. So if there are mouseup and a click...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found