I
I
Im p3l2021-06-10 15:10:46
React
Im p3l, 2021-06-10 15:10:46

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

1 answer(s)
B
BananaSupchik, 2021-06-10
@BananaSupchik

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 question

Ask a Question

731 491 924 answers to any question