-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: hooksのロジックを整理する #5361
base: master
Are you sure you want to change the base?
chore: hooksのロジックを整理する #5361
Conversation
…e-refactoring-hooks
commit: |
useEffect(() => { | ||
const handleClick = (e: MouseEvent) => { | ||
if (innerRefs.some((target) => isEventIncludedParent(e, target.current))) { | ||
innerCallback(e) | ||
|
||
return | ||
} | ||
|
||
outerCallback(e) | ||
}, | ||
// spread innerRefs to compare deps one by one | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[...innerRefs, innerCallback, outerCallback], | ||
) | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener('click', handleClick) | ||
|
||
return () => { | ||
window.removeEventListener('click', handleClick) | ||
} | ||
}, [handleClick]) | ||
}, [innerRefs, innerCallback, outerCallback]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleClickはuseEffectの中でしか利用されていないため、ひとまとめにしています。
依存関係の配列も展開は微妙なのでmemo化したものが渡されてくるように修正しています
if (!parent) return false | ||
|
||
const path = e.composedPath() | ||
if (path.length === 0 || !parent) return false | ||
|
||
if (path.length === 0) return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pathの生成が不要なパターンに対するチェックを最適化しています
useEffect(() => { | ||
const handleKeyPress = (e: KeyboardEvent) => { | ||
if (ESCAPE_KEY_REGEX.test(e.key)) { | ||
cb() | ||
} | ||
}, | ||
[cb], | ||
) | ||
useEffect(() => { | ||
} | ||
|
||
document.addEventListener('keydown', handleKeyPress) | ||
|
||
return () => document.removeEventListener('keydown', handleKeyPress) | ||
}, [handleKeyPress]) | ||
}, [cb]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleKeyPressはuseEffect中でしか利用されていないため、ひとまとめにしています
const handleOuterClick = useCallback( | ||
(e: MouseEvent) => { | ||
if (targets.some((target) => isEventIncludedParent(e, target.current))) { | ||
return | ||
useEffect(() => { | ||
const handleOuterClick = (e: MouseEvent) => { | ||
if (targets.every((target) => isEventExcludedParent(e, target.current))) { | ||
callback(e) | ||
} | ||
callback(e) | ||
}, | ||
// spread targets to compare deps one by one | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[...targets, callback], | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
早期returnが直後の処理の逆転条件でしかないため、理解がワンテンポ遅れることになり、デメリットしかない状態になっています。
条件を逆転させることでわかりやすくしています
const calculatedSeqs = useMemo(() => { | ||
const parentSeqs = parent.seqs.concat(currentSeq) | ||
|
||
return { | ||
parentSeqs, | ||
portalChildOf: parentSeqs.join(','), | ||
} | ||
}, [currentSeq, parent.seqs]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このファイルがやっていることはややこしいのですが、ざっくりいうと以下のとおりです
- アプリ全体でのPortalにシーケンスnoを振ります
- Portalを開く毎に紐づくnoをdata属性に追加し、現在開かれているportalが分かる状態にします
- 上記を利用して、特定のPortalがなにかのPortalの子になっているか?をチェックできるようにします
このチェックのため、data属性を利用していますが、data属性は基本stringしか突っ込めないので、joinでつなげるようにしています
const childOf = element.dataset?.portalChildOf || '' | ||
const includesSeq = childOf.split(',').includes(String(parentPortalSeq)) | ||
return includesSeq || _isChildPortal(element.parentElement, parentPortalSeq) | ||
|
||
let includesSeq = false | ||
const childOf = element.dataset?.portalChildOf | ||
|
||
if (childOf) { | ||
includesSeq = seqRegex.test(childOf) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これまでのロジックでは ,
つなぎの数値をsplitして比較していましたが、シーケンス番号は数値を,でつなげたものであるため、正規表現でsplitしなくてもチェック可能にしました。
…e-refactoring-hooks
関連URL
概要
変更内容
確認方法