Wrapping a input with a styled component has issues with onFocus/onBlur events
See original GitHub issueCurrent 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:
- Try to click on the input
- You see the isFocused state is changed and displays Focused
- 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:
- Created 4 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Which is an anti-pattern because React won’t even be able to know that
Another
is the same component between renders.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.