fix: support fetchPriority compatibility for React 18 and 19#503
fix: support fetchPriority compatibility for React 18 and 19#503sahsahvedov wants to merge 6 commits intoreact-component:masterfrom
Conversation
|
@test12376 is attempting to deploy a commit to the React Component Team on Vercel. A member of the Team first needs to authorize it. |
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
Walkthrough此PR添加了对HTML Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces support for the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for the fetchPriority prop, ensuring compatibility between React 18 and React 19 by dynamically using either fetchpriority or fetchPriority based on the React version. The changes include updating type definitions, adding a utility function to determine the correct prop, and adding relevant tests. My main feedback is to refactor a useMemo hook for better maintainability.
| () => { | ||
| const obj: ImageElementProps = {}; | ||
| COMMON_PROPS.forEach((prop: any) => { | ||
| if (props[prop] !== undefined) { | ||
| obj[prop] = props[prop]; | ||
| if (prop === 'fetchPriority') { | ||
| Object.assign(obj, getFetchPriorityProps(fetchPriority)); | ||
| } else if (prop === 'crossOrigin' && crossOrigin !== undefined) { | ||
| obj.crossOrigin = crossOrigin; | ||
| } else if (prop === 'decoding' && decoding !== undefined) { | ||
| obj.decoding = decoding; | ||
| } else if (prop === 'draggable' && draggable !== undefined) { | ||
| obj.draggable = draggable; | ||
| } else if (prop === 'loading' && loading !== undefined) { | ||
| obj.loading = loading; | ||
| } else if (prop === 'referrerPolicy' && referrerPolicy !== undefined) { | ||
| obj.referrerPolicy = referrerPolicy; | ||
| } else if (prop === 'sizes' && sizes !== undefined) { | ||
| obj.sizes = sizes; | ||
| } else if (prop === 'srcSet' && srcSet !== undefined) { | ||
| obj.srcSet = srcSet; | ||
| } else if (prop === 'useMap' && useMap !== undefined) { | ||
| obj.useMap = useMap; | ||
| } else if (prop === 'alt' && alt !== undefined) { | ||
| obj.alt = alt; | ||
| } | ||
| }); | ||
|
|
||
| return obj; | ||
| }, |
There was a problem hiding this comment.
This useMemo callback can be simplified to improve readability and maintainability. The current implementation uses a long if-else if chain, which is verbose and harder to maintain when adding or removing props.
Consider refactoring this to build the props object more dynamically by separating the generic props from the special case for fetchPriority.
() => {
const propsToPass = {
alt,
crossOrigin,
decoding,
draggable,
loading,
referrerPolicy,
sizes,
srcSet,
useMap,
};
const obj: Partial<ImageElementProps> = {};
for (const key in propsToPass) {
if (Object.prototype.hasOwnProperty.call(propsToPass, key) && propsToPass[key] !== undefined) {
obj[key] = propsToPass[key];
}
}
Object.assign(obj, getFetchPriorityProps(fetchPriority));
return obj;
},
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/Image.tsx (1)
172-202: 建议简化imgCommonProps的构建逻辑。当前实现遍历
COMMON_PROPS数组,但每个属性都使用硬编码的字符串比较进行处理。这使得循环变得冗余且难以维护。♻️ 建议的简化方案
const imgCommonProps = useMemo( () => { - const obj: ImageElementProps = {}; - COMMON_PROPS.forEach((prop: any) => { - if (prop === 'fetchPriority') { - Object.assign(obj, getFetchPriorityProps(fetchPriority)); - } else if (prop === 'crossOrigin' && crossOrigin !== undefined) { - obj.crossOrigin = crossOrigin; - } else if (prop === 'decoding' && decoding !== undefined) { - obj.decoding = decoding; - } else if (prop === 'draggable' && draggable !== undefined) { - obj.draggable = draggable; - } else if (prop === 'loading' && loading !== undefined) { - obj.loading = loading; - } else if (prop === 'referrerPolicy' && referrerPolicy !== undefined) { - obj.referrerPolicy = referrerPolicy; - } else if (prop === 'sizes' && sizes !== undefined) { - obj.sizes = sizes; - } else if (prop === 'srcSet' && srcSet !== undefined) { - obj.srcSet = srcSet; - } else if (prop === 'useMap' && useMap !== undefined) { - obj.useMap = useMap; - } else if (prop === 'alt' && alt !== undefined) { - obj.alt = alt; - } - }); - - return obj; + return { + ...(alt !== undefined && { alt }), + ...(crossOrigin !== undefined && { crossOrigin }), + ...(decoding !== undefined && { decoding }), + ...(draggable !== undefined && { draggable }), + ...(loading !== undefined && { loading }), + ...(referrerPolicy !== undefined && { referrerPolicy }), + ...(sizes !== undefined && { sizes }), + ...(srcSet !== undefined && { srcSet }), + ...(useMap !== undefined && { useMap }), + ...getFetchPriorityProps(fetchPriority), + }; }, [alt, crossOrigin, decoding, draggable, fetchPriority, loading, referrerPolicy, sizes, srcSet, useMap], );这种方式更简洁,且不依赖于
COMMON_PROPS数组的遍历。🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Image.tsx` around lines 172 - 202, The imgCommonProps builder is overly verbose by iterating COMMON_PROPS and hardcoding property checks; simplify by directly constructing the ImageElementProps object using the known prop variables and only assigning those that are defined (handle fetchPriority via getFetchPriorityProps(fetchPriority)), i.e., create obj with conditional assignments for alt, crossOrigin, decoding, draggable, loading, referrerPolicy, sizes, srcSet, useMap and merge getFetchPriorityProps when fetchPriority is present, replacing the COMMON_PROPS loop in the imgCommonProps useMemo.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Preview/index.tsx`:
- Line 197: The imgRef is created as useRef<HTMLImageElement>(null) which yields
RefObject<HTMLImageElement> but useImageTransform, useMouseEvent, useTouchEvent
and the PreviewImageProps interface currently expect
MutableRefObject<HTMLImageElement>, causing a type mismatch; update the
parameter and prop types in useImageTransform, useMouseEvent, useTouchEvent and
PreviewImageProps to accept RefObject<HTMLImageElement> (or HTMLImageElement |
null where appropriate) so callers can pass imgRef directly, and ensure any
internal reads guard for null (e.g., check imgRef.current before accessing
offsetWidth/offsetHeight/getBoundingClientRect); alternatively, if you prefer
minimal change, add a type assertion where imgRef is passed (e.g., imgRef as
unknown as MutableRefObject<HTMLImageElement>), but the preferred fix is
changing the hooks and interface signatures to RefObject<HTMLImageElement>.
---
Nitpick comments:
In `@src/Image.tsx`:
- Around line 172-202: The imgCommonProps builder is overly verbose by iterating
COMMON_PROPS and hardcoding property checks; simplify by directly constructing
the ImageElementProps object using the known prop variables and only assigning
those that are defined (handle fetchPriority via
getFetchPriorityProps(fetchPriority)), i.e., create obj with conditional
assignments for alt, crossOrigin, decoding, draggable, loading, referrerPolicy,
sizes, srcSet, useMap and merge getFetchPriorityProps when fetchPriority is
present, replacing the COMMON_PROPS loop in the imgCommonProps useMemo.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 103f7020-0ebc-429c-b881-95274a7e344d
📒 Files selected for processing (7)
src/Image.tsxsrc/Preview/index.tsxsrc/common.tssrc/interface.tssrc/util.tstests/fetchPriority.test.tsxtests/preview.test.tsx
|
@aojunhao123 Can you please merge this pr, need to ant-design/ant-design#57335 |
|
Thanks for the PR! I've noticed this issue too and landed a simpler fix in #504 — just adding fetchPriority to the existing |
Summary by CodeRabbit
发布说明
新功能
改进
测试