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.

Unable to set focus programmatically

See original GitHub issue

I use a controlled approach, so I store code in state and reset it in case of a verification error.

  const [code, setCode] = useState('') 
  const codeInputRef = useRef(null)

const onFulfill = (code) => {
  setCode('')
  codeInputRef.current.focusField(0)
}

render () {
  return (
      <OTPInputView
        ref={innerRef}
        autoFocusOnLoad
        onCodeFilled={onFulfill}
            onCodeChanged={setCode}
            code={code}
            innerRef={codeInputRef}
      />
  )
}

This seems to be working in the simulator, unfortunately it doesn’t work on a real device. Tested on iPhone 11 Pro

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
IPbiancocommented, Apr 30, 2020

The following seems to work:

resendCode() {
  this.setState({ code: '' });
  this._otpInput.focusField(0);
}

updateRef = (ref) => {
  this._otpInput = ref;
};

<OTPInputView
  ref={this.updateRef}
  ...
  ...
/>
0reactions
Dartangelcommented, Aug 1, 2022
It's working for me. I used libraries @react-native-community/hooks for focus effect when we go to backgond and return to the app

const currentAppState = useAppState();

useFocusEffect(useCallback(() => {
    setTimeout(() => {
        otpRef?.focusField(0);
    }, 500);
}, [currentAppState]));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't set focus on an input box programmatically in Angular
The problem is that the input element is still hidden when you try to set focus on it. To make sure that the...
Read more >
Unable to set focus on lightning-input-field in LWC
The focus() method does not work currently in the LWC Local Web Development Server. You'll need to deploy your code to an org...
Read more >
Set focus to input text box with JavaScript/jQuery | Techie Delight
This post will discuss how to set focus on the input text box with ... sets focus only at the page load time...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
blur() set/unset the focus on the element. For instance, let's make the visitor unable to leave the input if the value is invalid:....
Read more >
How to set focus on TextField whic… | Apple Developer Forums
That's because ViewDidAppear occurs later, when all responders are set. Don't forget to mark the thread as closed. Posted 4 years ago by....
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