Skip to content

Commit a7faa48

Browse files
authored
⚡ React.forwardRef 代码优化 (#734)
1 parent 674a9ed commit a7faa48

1 file changed

Lines changed: 46 additions & 30 deletions

File tree

src/pages/options/routes/ScriptList.tsx

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,22 @@ const MemoizedAvatar = React.memo(
119119
);
120120
MemoizedAvatar.displayName = "MemoizedAvatar";
121121

122-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
123-
const DraggableRow = ({ record, index, ...rest }: { record: any; index: any; [key: string]: any }) => {
122+
function composeRefs<T>(...refs: React.Ref<T>[]): (node: T | null) => void {
123+
return (node) => {
124+
for (const ref of refs) {
125+
if (typeof ref === "function") {
126+
ref(node);
127+
} else if (ref) {
128+
(ref as React.MutableRefObject<T | null>).current = node;
129+
}
130+
}
131+
};
132+
}
133+
134+
const DraggableRow = React.forwardRef<
135+
HTMLTableRowElement,
136+
{ record: any; index: any } & React.HTMLAttributes<HTMLTableRowElement>
137+
>(({ record, index: _index, ...rest }, ref) => {
124138
const sortable = useSortable({ id: record.uuid });
125139
const { setNodeRef, transform, transition } = sortable;
126140

@@ -131,10 +145,10 @@ const DraggableRow = ({ record, index, ...rest }: { record: any; index: any; [ke
131145

132146
return (
133147
<SortableRowCtx.Provider value={sortable}>
134-
<tr ref={setNodeRef} style={style} {...rest} />
148+
<tr ref={composeRefs(setNodeRef, ref)} style={style} {...rest} />
135149
</SortableRowCtx.Provider>
136150
);
137-
};
151+
});
138152
DraggableRow.displayName = "DraggableRow";
139153

140154
const DragHandle = () => {
@@ -162,32 +176,34 @@ const DragHandle = () => {
162176
);
163177
};
164178

165-
const DraggableContainer = React.forwardRef((props: any, ref: any) => {
166-
const context = useContext(DraggableContext);
167-
if (!context) return <></>;
168-
const { sensors, dispatch, scriptList } = context;
169-
return (
170-
<DndContext
171-
sensors={sensors}
172-
collisionDetection={closestCenter}
173-
modifiers={[restrictToVerticalAxis]}
174-
accessibility={{ container: document.body }}
175-
onDragEnd={(event: DragEndEvent) => {
176-
const { active, over } = event;
177-
if (!over) {
178-
return;
179-
}
180-
if (active.id !== over.id) {
181-
dispatch(sortScript({ active: active.id as string, over: over.id as string }));
182-
}
183-
}}
184-
>
185-
<SortableContext items={scriptList.map((s) => ({ ...s, id: s.uuid }))} strategy={verticalListSortingStrategy}>
186-
<tbody {...props} ref={ref} />
187-
</SortableContext>
188-
</DndContext>
189-
);
190-
});
179+
const DraggableContainer = React.forwardRef<HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(
180+
(props, ref) => {
181+
const context = useContext(DraggableContext);
182+
if (!context) return <></>;
183+
const { sensors, dispatch, scriptList } = context;
184+
return (
185+
<DndContext
186+
sensors={sensors}
187+
collisionDetection={closestCenter}
188+
modifiers={[restrictToVerticalAxis]}
189+
accessibility={{ container: document.body }}
190+
onDragEnd={(event: DragEndEvent) => {
191+
const { active, over } = event;
192+
if (!over) {
193+
return;
194+
}
195+
if (active.id !== over.id) {
196+
dispatch(sortScript({ active: active.id as string, over: over.id as string }));
197+
}
198+
}}
199+
>
200+
<SortableContext items={scriptList.map((s) => ({ ...s, id: s.uuid }))} strategy={verticalListSortingStrategy}>
201+
<tbody ref={ref} {...props} />
202+
</SortableContext>
203+
</DndContext>
204+
);
205+
}
206+
);
191207
DraggableContainer.displayName = "DraggableContainer";
192208

193209
const EnableSwitch = React.memo(

0 commit comments

Comments
 (0)