@@ -4,6 +4,7 @@ import React, {
44 useContext ,
55 useEffect ,
66 useState ,
7+ useMemo ,
78} from 'react'
89import classNames from 'classnames'
910import { View } from '@tarojs/components'
@@ -27,19 +28,16 @@ export const Col: FunctionComponent<Partial<TaroColProps>> = (props) => {
2728 ...defaultProps ,
2829 ...props ,
2930 }
30- const [ colName , setColName ] = useState ( '' )
31- const [ colStyle , setColStyle ] = useState ( { } )
3231 const { gutter } = useContext ( DataContext ) as any
3332
34- const classs = ( ) => {
35- // 定义col的class类
33+ const classs = useMemo ( ( ) => {
3634 const prefixCls = 'nut-col'
3735 return `${ prefixCls } ${ prefixCls } -${ span } ${
3836 gutter ? `${ prefixCls } -gutter` : ''
3937 } ${ prefixCls } -offset-${ offset } `
40- }
41- const getStyle = ( ) => {
42- // 定义col的style类
38+ } , [ offset , span , gutter ] )
39+
40+ const getStyle = useMemo ( ( ) => {
4341 const style : CSSProperties = { }
4442 if ( ! isFirst ) {
4543 style . paddingLeft = pxTransform ( ( gutter as number ) / 2 )
@@ -48,11 +46,15 @@ export const Col: FunctionComponent<Partial<TaroColProps>> = (props) => {
4846 style . paddingRight = pxTransform ( ( gutter as number ) / 2 )
4947 }
5048 return style
51- }
49+ } , [ isFirst , isLast , gutter ] )
50+
51+ const [ colName , setColName ] = useState ( classs )
52+ const [ colStyle , setColStyle ] = useState ( getStyle )
53+
5254 useEffect ( ( ) => {
5355 setColName ( classs )
5456 setColStyle ( getStyle )
55- } , [ span , offset , gutter ] )
57+ } , [ classs , getStyle ] )
5658
5759 return (
5860 < View
0 commit comments