Answer the question
In order to leave comments, you need to log in
How to get out of the infinite renderer?
When blur I want the blurEvent function to be executed , it is executed but then goes into an infinite loop due to inputRef.current.blur(); . But I need to get out of focus somehow. How can this problem be solved?
const inputRef = React.useRef(null);
const blurEvent= () => {
inputRef.current.focus({
cursor: 'start',
})
inputRef.current.blur();
}
<Input
placeholder="Name"
required
ref={inputRef}
onBlur={blurEvent}
/>
Answer the question
In order to leave comments, you need to log in
I hope it works
let isMyBlur = false;
const inputRef = React.useRef(null);
const blurEvent= () => {
if(!isMyBlur){
inputRef.current.focus({
cursor: 'start',
})
isMyBlur = true;
inputRef.current.blur();
}
isMyBlur = false;
}
<Input
placeholder="Name"
required
ref={inputRef}
onBlur={blurEvent}
/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question