Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Using React useRef() to apply focus to a TextArea inside a Popover when opened

I have an AntDesign Popover that contains a TextArea.

<Popover trigger="click" content={footnoteContent} title="Footnote" visible={footnotePopoverVisible} onVisibleChange={handleFootnoteVisibleChange} >
  ... a custom Button...
</Popover>

The const used for the content is as follows:

  const footnoteContent = (
    <Form form={form}>
      <Form.Item name={`${cellData.key}-fn`} initialValue={initialValue}>
        <Tooltip placement="left" title="Press Ctrl + Enter to close this window.">
        <Input.TextArea
          disabled={disabled}
          bordered={false}
          autoComplete="off"
          style={{ width: "300px" }}
          autoSize={{ minRows: 4 }}
          placeholder="Enter footnote"
          onBlur={() => save(cellData.key, form.getFieldValue(`${cellData.key}-fn`))}
          onKeyDown={handleKeyDown}
          autoFocus={true}
          ref={ref => { inputRef.current = ref; }}
        />
        </Tooltip>
      </Form.Item>
    </Form>
  );

I understand there is a lot going on here. However, note that I have set autoFocus on, and as expected, the first time I open the Popover the TextArea does in fact get focus. However, any subsequent clicks to open that same Popover and the TextArea is no longer automatically getting focus. I investigated and discovered useRef() hooks should help.

So, near the top of my Functional Component I have declared:

const inputRef = useRef();

and note that within my TextArea I have set the ref as follows (following some examples I found online):

ref={ref => { inputRef.current = ref; }}

The popover code above has a call to a function onVisibleChange which points to this function:

  function handleFootnoteVisibleChange (visible) {
    setFootnotePopoverVisible(visible);
    if (visible) {
      if (inputRef.current) {
        console.log(inputRef.current);
        inputRef.current.focus();
      }
    }
  }

Which I hoped would just re-focus the text area when the popover is made visible. However, nothing seems to happen. When I inspect the session and check the console.log(inputRef.current); above, there seems to be a focus() function to call... yet nothing is happening.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

With useRef, apply the ref direct to your input field.

const myRef = useRef();
...
<MyInput ref={myRef} ...

The method you showed is legacy ref assignment from class based components. useRef works different.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!