-
-
Notifications
You must be signed in to change notification settings - Fork 684
fix: 修复useActive 中 items 为空数组导致的异常 #824
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
base: main
Are you sure you want to change the base?
Conversation
📝 Walkthrough""" Walkthrough本次更改在 Changes
Poem
Tip ⚡️ Faster reviews with caching
Enjoy the performance boost—your workflow just got faster. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Walkthrough: 该PR修复了在 Changes:
|
components/suggestion/useActive.ts
Outdated
@@ -117,6 +117,7 @@ export default function useActive( | |||
}); | |||
|
|||
React.useEffect(() => { | |||
if(items?.length === 0) return; |
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.
添加了对items
为空数组的检查,确保在items
为空时提前返回,避免后续对空数组的操作。这是一个重要的修复,防止了潜在的运行时错误。
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
components/suggestion/useActive.ts (1)
119-124
: 逻辑修复正确,防止了空数组导致的异常修改增加了一个守卫条件,当
items
为空数组或未定义时直接返回,避免了在items
为空时访问items[0].value
导致的类型错误。这是一个必要的防御性编程措施。不过有一点小建议:可以考虑使用
if (!items?.length)
作为条件判断,这样更简洁,且同样能处理items
为undefined
/null
的情况。- if(items?.length === 0) return; + if (!items?.length) return;
// 确保 items 是一个数组且至少有一个元素 | ||
if (!Array.isArray(items) || items.length === 0) return; | ||
if (open) { | ||
setActivePaths([items[0].value]); | ||
} |
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.
// 确保 items 是一个数组且至少有一个元素 | |
if (!Array.isArray(items) || items.length === 0) return; | |
if (open) { | |
setActivePaths([items[0].value]); | |
} | |
if (open && Array.isArray(items) && items.length > 0) { | |
setActivePaths([items[0].value]); | |
} |
补充一个测试用例吧。 |
Bundle ReportChanges will increase total bundle size by 232.88kB (171.19%) ⬆️
Affected Assets, Files, and Routes:view changes for bundle: antdx-array-pushAssets Changed:
|
Summary by CodeRabbit