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.

TextInput component lose's ref

See original GitHub issue

The 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

image

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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
coltonwcommented, Jun 19, 2020

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.

0reactions
ShimiSuncommented, Oct 21, 2020

Thanks @IanKBovard & @lllllllqw for collaborating on this, @lllllllqw feel free to make code contributions that will help to improve grommet Class component ref support.

Read more comments on GitHub >

github_iconTop 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 >

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