TextInput component lose's ref
See original GitHub issueThe use of ref on TextInput does not work as expected. When I try to focus input on first render I get error, because of ref is undefined
const StyledTextInput = styled(TextInput)`
border: ${({ theme }) => `1px solid ${theme.global.colors.paletteGray10}`};
padding-top: ${({ theme }) => theme.global.edgeSize.xsmall};
padding-bottom: ${({ theme }) => theme.global.edgeSize.xsmall};
border-radius: 0.3rem;
width: 100%;
`;
const searchRef = useRef();
useEffect(() => {
searchRef.current.focus();
}, []);
<StyledTextInput
ref={searchRef}
type="text"
onChange={onChangeTextInput}
value={searchText}
/>
When I tryed to use simple <input>
all works as expected
<input
ref={searchRef}
type="text"
onChange={onChangeTextInput}
value={searchText}
placeholder={searchPlaceholder}
data-automation-id={`${dataAutomationId}-search`}
/>
Expected Behavior
ref should point on TextInput
Actual Behavior
URL, screen shot, or Codepen exhibiting the issue
Steps to Reproduce
Your Environment
- Grommet version: v2.10.0
- Browser Name and version: Chrome
- Operating System and version (desktop or mobile): macOs
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:16 (11 by maintainers)
Top Results From Across the Web
React: Losing ref values - Stack Overflow
Child components are rendered; I am entering one of input fields and get out (this invokes onBlur() method) - state gets updated, child ......
Read more >React Text Input Losing Focus After Each Keypress
React Text Input Losing Focus After Each Keypress. This bug took me a while to figure out, so I thought I would share...
Read more >TextInput - ReactXP
This component provides basic text input capabilities. It can be used in one of two modes. In the first mode, the contents of...
Read more >Lifting State Up - React
In React, this is usually solved by making a component “controlled”. Just like the DOM <input> accepts both a value and an onChange...
Read more >A complete guide to React refs - LogRocket Blog
The issue now is that passing a ref to this component will return its instance, a React component reference, and not the input...
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
https://codesandbox.io/s/cannot-read-property-focus-of-undefined-j0cjp
Seems like it is just failing in our unit tests because our unit tests didn’t have a theme wrapper before. If you wrap it in a Grommet component with a theme, it works fine. Not sure how defensive you want to code that but figured it was worth noting at least for future developers.
Thanks @IanKBovard & @lllllllqw for collaborating on this, @lllllllqw feel free to make code contributions that will help to improve grommet Class component
ref
support.