Skip to content

fix(range): taro环境异步渲染useReady不会触发 #3297

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

Open
wants to merge 3 commits into
base: feat_v3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/packages/avatarcropper/avatarcropper.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
useMemo,
useCallback,
FunctionComponent,
useLayoutEffect,
} from 'react'
import Taro, { useReady, createSelectorQuery } from '@tarojs/taro'
import Taro, { createSelectorQuery } from '@tarojs/taro'
import classNames from 'classnames'
import { Canvas, CommonEventFunction, View } from '@tarojs/components'
import { getWindowInfo } from '@/utils/taro/get-system-info'
Expand Down Expand Up @@ -137,7 +138,7 @@
cropperCanvasContext: null,
})

useReady(() => {
useLayoutEffect(() => {
if (showAlipayCanvas2D) {
const { canvasId } = canvasAll
createSelectorQuery()
Expand All @@ -149,14 +150,14 @@
})
.exec()
}
})
}, [showAlipayCanvas2D, state.displayHeight, state.displayWidth])

Check warning on line 153 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useLayoutEffect has a missing dependency: 'canvasAll'. Either include it or remove the dependency array. You can also do a functional update 'setCanvasAll(c => ...)' if you only need 'canvasAll' in the 'setCanvasAll' call

useEffect(() => {
setCanvasAll({
...canvasAll,
cropperCanvasContext: Taro.createCanvasContext(canvasAll.canvasId),
})
}, [])

Check warning on line 160 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'canvasAll'. Either include it or remove the dependency array. You can also do a functional update 'setCanvasAll(c => ...)' if you only need 'canvasAll' in the 'setCanvasAll' call

const touch = useTouch()

Expand All @@ -168,7 +169,7 @@
height,
borderRadius: shape === 'round' ? '50%' : '',
}
}, [pixelRatio, state.cropperWidth])

Check warning on line 172 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useMemo has a missing dependency: 'shape'. Either include it or remove the dependency array

// 是否是横向
const isAngle = useMemo(() => {
Expand Down Expand Up @@ -250,7 +251,7 @@
ctx.scale(scale, scale)
ctx.drawImage(src as HTMLImageElement, x, y, width, height)
},
[drawImage, state]

Check warning on line 254 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has missing dependencies: 'pixelRatio' and 'space'. Either include them or remove the dependency array
)

// web绘制
Expand All @@ -267,7 +268,7 @@
canvas.height = state.displayHeight
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
canvas2dDraw(ctx)
}, [canvas2dDraw])

Check warning on line 271 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has missing dependencies: 'canvasAll.canvasId', 'state.displayHeight', and 'state.displayWidth'. Either include them or remove the dependency array

const alipayDraw = useCallback(() => {
const ctx = canvasAll.cropperCanvas.getContext(
Expand Down Expand Up @@ -324,7 +325,7 @@
ctx.scale(scale, scale)
ctx.drawImage(src as string, x, y, width, height)
ctx.draw()
}, [drawImage, state.scale, state.angle, state.moveX, state.moveY])

Check warning on line 328 in src/packages/avatarcropper/avatarcropper.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has missing dependencies: 'alipayDraw', 'canvasAll', 'showAlipayCanvas2D', 'space', 'state', and 'webDraw'. Either include them or remove the dependency array

useEffect(() => {
if (Math.abs(state.moveX) > maxMoveX) {
Expand Down Expand Up @@ -693,4 +694,5 @@
</>
)
}

AvatarCropper.displayName = 'NutAvatarCropper'
25 changes: 13 additions & 12 deletions src/packages/lottie/lottiemp.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useImperativeHandle, useRef } from 'react'
import { createSelectorQuery, getEnv, useReady, useUnload } from '@tarojs/taro'
import React, { useImperativeHandle, useLayoutEffect, useRef } from 'react'
import { createSelectorQuery, getEnv, useUnload } from '@tarojs/taro'
import lottie from '@nutui/lottie-miniprogram'
import { getWindowInfo } from '@/utils/taro/get-system-info'
import { useUuid } from '@/hooks/use-uuid'
Expand All @@ -20,15 +20,10 @@ export const Lottie = React.forwardRef((props: TaroLottieProps, ref: any) => {
speed = 1,
dpr = true,
} = props
const setSpeed = () => {
if (animation.current) {
animation.current.setSpeed(Math.abs(speed))
animation.current.setDirection(speed > 0 ? 1 : -1)
}
}

useImperativeHandle(ref, () => animation.current || {})
const pixelRatio = useRef(getWindowInfo().pixelRatio)
useReady(() => {
useLayoutEffect(() => {
createSelectorQuery()
.select(`#${id}`)
.fields(
Expand Down Expand Up @@ -62,17 +57,23 @@ export const Lottie = React.forwardRef((props: TaroLottieProps, ref: any) => {
context,
},
})
onComplete &&
if (onComplete) {
animation.current.addEventListener('complete', onComplete)
setSpeed()
}

if (animation.current) {
animation.current.setSpeed(Math.abs(speed))
animation.current.setDirection(speed > 0 ? 1 : -1)
}
inited.current = true
} catch (error) {
console.error(error)
}
}
)
.exec()
})
}, [autoPlay, dpr, id, loop, onComplete, source, speed, style])

useUnload(() => {
onComplete &&
animation.current &&
Expand Down
16 changes: 6 additions & 10 deletions src/packages/range/range.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, {
FunctionComponent,
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react'
import classNames from 'classnames'
import { Text, View } from '@tarojs/components'
import { useReady, nextTick } from '@tarojs/taro'
import { pxTransform } from '@/utils/taro/px-transform'
import { useTouch } from '@/hooks/use-touch'
import { ComponentDefaults } from '@/utils/typings'
Expand Down Expand Up @@ -286,17 +286,13 @@ export const Range: FunctionComponent<
[innerValue, disabled, isRange, min, scope, updateValue, vertical]
)

useReady(() => {
const getRootRect = async () => {
if (root.current) {
const rect = await getRectInMultiPlatform(root.current)
useLayoutEffect(() => {
if (root.current) {
getRectInMultiPlatform(root.current).then((rect) => {
rootRect.current = rect
}
})
}
nextTick(() => {
getRootRect()
})
})
}, [])

const onTouchStart = useCallback(
(event: any) => {
Expand Down
11 changes: 6 additions & 5 deletions src/packages/rate/rate.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, {
FunctionComponent,
ReactElement,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react'
import classNames from 'classnames'
import { StarFill } from '@nutui/icons-react-taro'
import { useReady } from '@tarojs/taro'
import { ITouchEvent, Text, View } from '@tarojs/components'
import { ComponentDefaults } from '@/utils/typings'
import { usePropsValue } from '@/hooks/use-props-value'
Expand Down Expand Up @@ -131,7 +132,7 @@ export const Rate: FunctionComponent<Partial<TaroRateProps>> = (props) => {
}
}

const updateRects = () => {
const updateRects = useCallback(() => {
for (let index = 0; index < refs.length; index++) {
const item = refs[index]
if (item) {
Expand All @@ -140,11 +141,11 @@ export const Rate: FunctionComponent<Partial<TaroRateProps>> = (props) => {
})
}
}
}
}, [refs])

useReady(() => {
useLayoutEffect(() => {
updateRects()
})
}, [updateRects])

const handleTouchStart = (e: any) => {
if (!touchable || readOnly || disabled) {
Expand Down
74 changes: 40 additions & 34 deletions src/packages/swipe/swipe.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, {
forwardRef,
MouseEvent,
useCallback,
useEffect,
useImperativeHandle,
useLayoutEffect,
useRef,
useState,
} from 'react'
import classNames from 'classnames'
import { ITouchEvent, View } from '@tarojs/components'
import { BaseEventOrig } from '@tarojs/components/types/common'
import { nextTick, useReady } from '@tarojs/taro'
import { useTouch } from '@/hooks/use-touch'
import { getRectInMultiPlatform } from '@/utils/taro/get-rect'
import { ComponentDefaults } from '@/utils/typings'
Expand Down Expand Up @@ -45,8 +46,43 @@ export const Swipe = forwardRef<
const leftId = `swipe-left-${uid}`
const rightId = `swipe-right-${uid}`

const { children, className, style } = { ...defaultProps, ...props }

const root: any = useRef<HTMLDivElement>()
const opened = useRef(false)
const lockClick = useRef(false)
const startOffset = useRef(0)

const [state, setState] = useState({
offset: 0,
dragging: false,
})

const [actionWidth, updateState] = useRefState({
left: 0,
right: 0,
})
const setActionWidth = useCallback(
(fn: any) => {
const res = fn()
if (res.left !== undefined) {
updateState({
...actionWidth.current,
left: res.left,
})
}
if (res.right !== undefined) {
updateState({
...actionWidth.current,
right: res.right,
})
}
},
[actionWidth, updateState]
)

// 获取元素的时候要在页面 onReady 后,需要参考小程序的事件周期
useReady(() => {
useLayoutEffect(() => {
const getWidth = async () => {
if (leftWrapper.current) {
const leftRect = await getRectInMultiPlatform(
Expand All @@ -64,40 +100,10 @@ export const Swipe = forwardRef<
setActionWidth((v: any) => ({ ...v, right: rightRect.width }))
}
}
nextTick(() => getWidth())
})

const { children, className, style } = { ...defaultProps, ...props }
getWidth()
}, [leftId, rightId, setActionWidth])

const root: any = useRef<HTMLDivElement>()
const opened = useRef(false)
const lockClick = useRef(false)
const startOffset = useRef(0)

const [state, setState] = useState({
offset: 0,
dragging: false,
})

const [actionWidth, updateState] = useRefState({
left: 0,
right: 0,
})
const setActionWidth = (fn: any) => {
const res = fn()
if (res.left !== undefined) {
updateState({
...actionWidth.current,
left: res.left,
})
}
if (res.right !== undefined) {
updateState({
...actionWidth.current,
right: res.right,
})
}
}
const wrapperStyle = {
transform: `translate(${state.offset}${!harmony() ? 'px' : ''}, 0)`,
transitionDuration: state.dragging ? '0s' : '.6s',
Expand Down
Loading