Skip to content

Commit 80baafd

Browse files
committed
fix(ranking): 优化性能
1 parent 13c29a7 commit 80baafd

3 files changed

Lines changed: 43 additions & 23 deletions

File tree

src/content/pages/ranking/App.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ const App: FC = () => {
5050
)}
5151
{(showPredictordelta || showNewRating) &&
5252
rows?.map((row, i) => (
53-
<Portal container={row} key={i}>
54-
<td>
55-
<Item
56-
row={i}
57-
hasMyRank={hasMyRank}
58-
showOldRating={showOldRating}
59-
showPredictordelta={showPredictordelta}
60-
showNewRating={showNewRating}
61-
/>
62-
</td>
63-
</Portal>
53+
<Item
54+
key={i}
55+
row={row}
56+
index={i}
57+
hasMyRank={hasMyRank}
58+
showOldRating={showOldRating}
59+
showPredictordelta={showPredictordelta}
60+
showNewRating={showNewRating}
61+
/>
6462
))}
6563
{showLanguageIcon &&
6664
rows?.map((row, i) => (

src/content/pages/ranking/Item.tsx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { FC, useEffect, useState } from 'react'
1+
import { Portal } from '@/components/Portal'
2+
import { FC, memo, useEffect, useState } from 'react'
23
import { css } from 'styled-components/macro'
34

45
import { debounce } from '../../../utils'
56

67
import { ParamType, useGetPredictionQuery } from './rankSlice'
78

89
type ItmeType = {
9-
row: number
10+
index: number
1011
hasMyRank: boolean
1112
showOldRating: boolean
1213
showPredictordelta: boolean
@@ -52,13 +53,13 @@ function useUrlChange() {
5253
return [param] as const
5354
}
5455

55-
const Item: FC<ItmeType> = ({
56-
row,
56+
const Item: FC<ItmeType> = memo(function Item({
57+
index,
5758
hasMyRank,
5859
showOldRating,
5960
showPredictordelta,
6061
showNewRating,
61-
}) => {
62+
}) {
6263
const [param] = useUrlChange()
6364
const params: ParamType = { ...param }
6465
if (hasMyRank) {
@@ -72,9 +73,9 @@ const Item: FC<ItmeType> = ({
7273
return <span> ...loading</span>
7374
}
7475

75-
const predictor: number | undefined = items?.[row]?.delta?.toFixed(1),
76-
newRating = items?.[row]?.newRating?.toFixed(1),
77-
oldRating = items?.[row]?.oldRating?.toFixed(1)
76+
const predictor: number | undefined = items?.[index]?.delta?.toFixed(1),
77+
newRating = items?.[index]?.newRating?.toFixed(1),
78+
oldRating = items?.[index]?.oldRating?.toFixed(1)
7879

7980
if (predictor === undefined) {
8081
return <></>
@@ -123,6 +124,27 @@ const Item: FC<ItmeType> = ({
123124
)}
124125
</div>
125126
)
127+
})
128+
interface PredictItemProps {
129+
row: HTMLElement
130+
index: number
131+
hasMyRank: boolean
132+
showOldRating: boolean
133+
showPredictordelta: boolean
134+
showNewRating: boolean
126135
}
127136

128-
export default Item
137+
const PredictItem = memo(function PredictItem({
138+
row,
139+
...props
140+
}: PredictItemProps) {
141+
return (
142+
<Portal container={row}>
143+
<td>
144+
<Item {...props} />
145+
</td>
146+
</Portal>
147+
)
148+
})
149+
150+
export default PredictItem

src/content/pages/ranking/LanguageIcon.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useEffect, useState } from 'react'
1+
import { FC, memo, useEffect, useState } from 'react'
22
import styled from 'styled-components/macro'
33

44
import { debounce } from '../../../utils'
@@ -144,7 +144,7 @@ export const LanguageIconRow: FC<{
144144
row: HTMLElement
145145
i: number
146146
hasMyRank: boolean
147-
}> = ({ row, i, hasMyRank }) => {
147+
}> = memo(function LanguageIconRow({ row, i, hasMyRank }) {
148148
const tds = Array.prototype.slice.call(row.children, 4, 8)
149149
return (
150150
<>
@@ -159,6 +159,6 @@ export const LanguageIconRow: FC<{
159159
))}
160160
</>
161161
)
162-
}
162+
})
163163

164164
export default LanguageIcon

0 commit comments

Comments
 (0)