Skip to content

Commit ab427d9

Browse files
committed
refactor: 将组件中使用的useReady替换为useLayoutEffect
1 parent 1e14c62 commit ab427d9

File tree

5 files changed

+67
-62
lines changed

5 files changed

+67
-62
lines changed

src/packages/avatarcropper/avatarcropper.taro.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import React, {
44
useMemo,
55
useCallback,
66
FunctionComponent,
7+
useLayoutEffect,
78
} from 'react'
8-
import Taro, { useReady, createSelectorQuery } from '@tarojs/taro'
9+
import Taro, { createSelectorQuery } from '@tarojs/taro'
910
import classNames from 'classnames'
1011
import { Canvas, CommonEventFunction, View } from '@tarojs/components'
1112
import { getWindowInfo } from '@/utils/taro/get-system-info'
@@ -137,7 +138,7 @@ export const AvatarCropper: FunctionComponent<
137138
cropperCanvasContext: null,
138139
})
139140

140-
useReady(() => {
141+
useLayoutEffect(() => {
141142
if (showAlipayCanvas2D) {
142143
const { canvasId } = canvasAll
143144
createSelectorQuery()
@@ -149,7 +150,7 @@ export const AvatarCropper: FunctionComponent<
149150
})
150151
.exec()
151152
}
152-
})
153+
}, [showAlipayCanvas2D, state.displayHeight, state.displayWidth])
153154

154155
useEffect(() => {
155156
setCanvasAll({
@@ -693,4 +694,5 @@ export const AvatarCropper: FunctionComponent<
693694
</>
694695
)
695696
}
697+
696698
AvatarCropper.displayName = 'NutAvatarCropper'

src/packages/lottie/lottiemp.taro.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useImperativeHandle, useRef } from 'react'
2-
import { createSelectorQuery, getEnv, useReady, useUnload } from '@tarojs/taro'
1+
import React, { useImperativeHandle, useLayoutEffect, useRef } from 'react'
2+
import { createSelectorQuery, getEnv, useUnload } from '@tarojs/taro'
33
import lottie from '@nutui/lottie-miniprogram'
44
import { getWindowInfo } from '@/utils/taro/get-system-info'
55
import { useUuid } from '@/hooks/use-uuid'
@@ -20,15 +20,10 @@ export const Lottie = React.forwardRef((props: TaroLottieProps, ref: any) => {
2020
speed = 1,
2121
dpr = true,
2222
} = props
23-
const setSpeed = () => {
24-
if (animation.current) {
25-
animation.current.setSpeed(Math.abs(speed))
26-
animation.current.setDirection(speed > 0 ? 1 : -1)
27-
}
28-
}
23+
2924
useImperativeHandle(ref, () => animation.current || {})
3025
const pixelRatio = useRef(getWindowInfo().pixelRatio)
31-
useReady(() => {
26+
useLayoutEffect(() => {
3227
createSelectorQuery()
3328
.select(`#${id}`)
3429
.fields(
@@ -62,17 +57,23 @@ export const Lottie = React.forwardRef((props: TaroLottieProps, ref: any) => {
6257
context,
6358
},
6459
})
65-
onComplete &&
60+
if (onComplete) {
6661
animation.current.addEventListener('complete', onComplete)
67-
setSpeed()
62+
}
63+
64+
if (animation.current) {
65+
animation.current.setSpeed(Math.abs(speed))
66+
animation.current.setDirection(speed > 0 ? 1 : -1)
67+
}
6868
inited.current = true
6969
} catch (error) {
7070
console.error(error)
7171
}
7272
}
7373
)
7474
.exec()
75-
})
75+
}, [autoPlay, dpr, id, loop, onComplete, source, speed, style])
76+
7677
useUnload(() => {
7778
onComplete &&
7879
animation.current &&

src/packages/range/range.taro.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import React, {
99
} from 'react'
1010
import classNames from 'classnames'
1111
import { Text, View } from '@tarojs/components'
12-
import { nextTick } from '@tarojs/taro'
1312
import { pxTransform } from '@/utils/taro/px-transform'
1413
import { useTouch } from '@/hooks/use-touch'
1514
import { ComponentDefaults } from '@/utils/typings'
@@ -288,15 +287,11 @@ export const Range: FunctionComponent<
288287
)
289288

290289
useLayoutEffect(() => {
291-
const getRootRect = async () => {
292-
if (root.current) {
293-
const rect = await getRectInMultiPlatform(root.current)
290+
if (root.current) {
291+
getRectInMultiPlatform(root.current).then((rect) => {
294292
rootRect.current = rect
295-
}
293+
})
296294
}
297-
nextTick(() => {
298-
getRootRect()
299-
})
300295
}, [])
301296

302297
const onTouchStart = useCallback(

src/packages/rate/rate.taro.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React, {
22
FunctionComponent,
33
ReactElement,
4+
useCallback,
45
useEffect,
6+
useLayoutEffect,
57
useRef,
68
useState,
79
} from 'react'
810
import classNames from 'classnames'
911
import { StarFill } from '@nutui/icons-react-taro'
10-
import { useReady } from '@tarojs/taro'
1112
import { ITouchEvent, Text, View } from '@tarojs/components'
1213
import { ComponentDefaults } from '@/utils/typings'
1314
import { usePropsValue } from '@/hooks/use-props-value'
@@ -131,7 +132,7 @@ export const Rate: FunctionComponent<Partial<TaroRateProps>> = (props) => {
131132
}
132133
}
133134

134-
const updateRects = () => {
135+
const updateRects = useCallback(() => {
135136
for (let index = 0; index < refs.length; index++) {
136137
const item = refs[index]
137138
if (item) {
@@ -140,11 +141,11 @@ export const Rate: FunctionComponent<Partial<TaroRateProps>> = (props) => {
140141
})
141142
}
142143
}
143-
}
144+
}, [refs])
144145

145-
useReady(() => {
146+
useLayoutEffect(() => {
146147
updateRects()
147-
})
148+
}, [updateRects])
148149

149150
const handleTouchStart = (e: any) => {
150151
if (!touchable || readOnly || disabled) {

src/packages/swipe/swipe.taro.tsx

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React, {
22
forwardRef,
33
MouseEvent,
4+
useCallback,
45
useEffect,
56
useImperativeHandle,
7+
useLayoutEffect,
68
useRef,
79
useState,
810
} from 'react'
911
import classNames from 'classnames'
1012
import { ITouchEvent, View } from '@tarojs/components'
1113
import { BaseEventOrig } from '@tarojs/components/types/common'
12-
import { nextTick, useReady } from '@tarojs/taro'
1314
import { useTouch } from '@/hooks/use-touch'
1415
import { getRectInMultiPlatform } from '@/utils/taro/get-rect'
1516
import { ComponentDefaults } from '@/utils/typings'
@@ -45,8 +46,43 @@ export const Swipe = forwardRef<
4546
const leftId = `swipe-left-${uid}`
4647
const rightId = `swipe-right-${uid}`
4748

49+
const { children, className, style } = { ...defaultProps, ...props }
50+
51+
const root: any = useRef<HTMLDivElement>()
52+
const opened = useRef(false)
53+
const lockClick = useRef(false)
54+
const startOffset = useRef(0)
55+
56+
const [state, setState] = useState({
57+
offset: 0,
58+
dragging: false,
59+
})
60+
61+
const [actionWidth, updateState] = useRefState({
62+
left: 0,
63+
right: 0,
64+
})
65+
const setActionWidth = useCallback(
66+
(fn: any) => {
67+
const res = fn()
68+
if (res.left !== undefined) {
69+
updateState({
70+
...actionWidth.current,
71+
left: res.left,
72+
})
73+
}
74+
if (res.right !== undefined) {
75+
updateState({
76+
...actionWidth.current,
77+
right: res.right,
78+
})
79+
}
80+
},
81+
[actionWidth, updateState]
82+
)
83+
4884
// 获取元素的时候要在页面 onReady 后,需要参考小程序的事件周期
49-
useReady(() => {
85+
useLayoutEffect(() => {
5086
const getWidth = async () => {
5187
if (leftWrapper.current) {
5288
const leftRect = await getRectInMultiPlatform(
@@ -64,40 +100,10 @@ export const Swipe = forwardRef<
64100
setActionWidth((v: any) => ({ ...v, right: rightRect.width }))
65101
}
66102
}
67-
nextTick(() => getWidth())
68-
})
69103

70-
const { children, className, style } = { ...defaultProps, ...props }
104+
getWidth()
105+
}, [leftId, rightId, setActionWidth])
71106

72-
const root: any = useRef<HTMLDivElement>()
73-
const opened = useRef(false)
74-
const lockClick = useRef(false)
75-
const startOffset = useRef(0)
76-
77-
const [state, setState] = useState({
78-
offset: 0,
79-
dragging: false,
80-
})
81-
82-
const [actionWidth, updateState] = useRefState({
83-
left: 0,
84-
right: 0,
85-
})
86-
const setActionWidth = (fn: any) => {
87-
const res = fn()
88-
if (res.left !== undefined) {
89-
updateState({
90-
...actionWidth.current,
91-
left: res.left,
92-
})
93-
}
94-
if (res.right !== undefined) {
95-
updateState({
96-
...actionWidth.current,
97-
right: res.right,
98-
})
99-
}
100-
}
101107
const wrapperStyle = {
102108
transform: `translate(${state.offset}${!harmony() ? 'px' : ''}, 0)`,
103109
transitionDuration: state.dragging ? '0s' : '.6s',

0 commit comments

Comments
 (0)