Fixes an issue that causes the focus to move whenever the state changes when using AutoFocus component
#1279
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Self Checklist
CODEOWNERSfile.Related Issue
None
Summary
state가 변할 때마다
AutoFocus의HTMLElement.focus()메서드가 실행되는 버그를 수정합니다.Details
기존의 ref callback을 사용한 구현을
useState,useLayoutEffect를 사용한 구현으로 변경했습니다.리액트 공식 문서를 참고해봤을 때, 함수의 참조가 동일하다면 함수가 매 렌더마다 detach - attach 되면 안되는 거로 예상했지만 실제 동작은 달랐습니다. 상태 변경 시 함수가 계속 실행되어
focus메서드가 매 렌더마다 실행되었습니다(re-attach).useState의setState함수를 ref callback으로 사용했을 경우엔 DOMNode가 Document에 attach 되었을 때 1번, detach 되었을 때 1번. 즉, 예상한 대로 동작하여useLayoutEffect와 함께 사용하는 방식으로 수정했습니다. 플리커링 방지를 위해(브라우저 Paint 시점 이전에 메서드를 실행함을 보장)useEffect대신useLayoutEffect를 사용했습니다.Slot과 결합되었을 때의 이슈로 추정됩니다.etc
useMergeRefs: 타입을 교정하고, 함수를 분리하여 가독성을 높였습니다. (참고)Breaking change or not (Yes/No)
No
References