11import React , {
22 forwardRef ,
33 MouseEvent ,
4+ useCallback ,
45 useEffect ,
56 useImperativeHandle ,
7+ useLayoutEffect ,
68 useRef ,
79 useState ,
810} from 'react'
911import classNames from 'classnames'
1012import { ITouchEvent , View } from '@tarojs/components'
1113import { BaseEventOrig } from '@tarojs/components/types/common'
12- import { nextTick , useReady } from '@tarojs/taro'
1314import { useTouch } from '@/hooks/use-touch'
1415import { getRectInMultiPlatform } from '@/utils/taro/get-rect'
1516import { 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