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.

Wrapping a input with a styled component has issues with onFocus/onBlur events

See original GitHub issue

Current behavior: I tried wrapping a input with a styled component like this:

 <Wrapper>
      <input
        type="text"
        onFocus={() => setIsFocused(true)}
        onBlur={() => setIsFocused(false)}
      />
      {isFocused && "test"}
</Wrapper>

I need to click twice on the input to focus and be able to write text.

To reproduce:

Link to Sandbox

  1. Try to click on the input
  2. You see the isFocused state is changed and displays Focused
  3. Click again to gain control of the input and now you can start writing

Expected behavior: The browser’s default behavior (click once)

Environment information:

  • react version: 16.12.0
  • @emotion/core version: “^10.0.28”,
  • @emotion/styled version: “^10.0.27”,

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Andaristcommented, Mar 11, 2020

You need to move out your Wrapper out of render. You are recreating a component’s type each render and thus React loses the whole state (including focus) for it. Fixed sandbox: https://codesandbox.io/s/styled-div-focus-issue-ojd5r

It’s basically like if you would do smth like this:

function Comp() {
  const Another = () => <input/>
  return <Another/>
}

Which is an anti-pattern because React won’t even be able to know that Another is the same component between renders.

1reaction
Andaristcommented, May 1, 2021

what makes you say that? I’d like to understand why. Declaring the components in the render functions results in much cleaner code in our project. Is it a performance concern?

Yes, it’s a performance concern and not because of Emotion but because of React. By declaring components in render you force them to remount which each rerender as React won’t be available to reconcile them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

styled-components onFocus and onBlur event does not work
If I understand your issue/question, it sounds like you want the dropdown to close when it loses focus. Edit - Use a "handle...
Read more >
Styled-Components Onfocus And Onblur Event ... - ADocLib
But the input loses focus and I can't attached a onFocus event on a div. ... Wrapping a input with a styled component...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
The focus event is called on focusing, and blur – when the ... color: red } </style> Your email please: <input type="email" id="input">...
Read more >
Plaid Inspired Inputs with React Hooks and Styled Components
We'll add single dependency - styled components - which has become ... @param {function} onBlur - function called when the input loses focus...
Read more >
Creating split OTP input fields in React Native - LogRocket Blog
We'll use styled-components for app styling, which you can install with: ... To handle the onBlur event when the input is out of...
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