1- import { useMemo , useRef } from 'react' ;
1+ import { useMemo } from 'react' ;
22import { StyleProp , ViewStyle , TextStyle , ImageStyle } from 'react-native' ;
33
44import { BaseTheme , RNStyle , Dimensions } from '../types' ;
@@ -15,25 +15,25 @@ const filterRestyleProps = <
1515 omitPropertiesMap : Record < keyof TProps , boolean > ,
1616) => {
1717 return getKeys ( props ) . reduce (
18- ( { cleanProps, restyleProps, serialized } , key ) => {
18+ ( { cleanProps, restyleProps, serializedRestyleProps } , key ) => {
1919 if ( omitPropertiesMap [ key as keyof TProps ] ) {
2020 return {
21- serialized : `${ serialized } ${ key } :${ props [ key ] } ;` ,
2221 cleanProps,
2322 restyleProps : { ...restyleProps , [ key ] : props [ key ] } ,
23+ serializedRestyleProps : `${ serializedRestyleProps } ${ key } :${ props [ key ] } ;` ,
2424 } ;
2525 } else {
2626 return {
27- serialized,
2827 cleanProps : { ...cleanProps , [ key ] : props [ key ] } ,
2928 restyleProps,
29+ serializedRestyleProps,
3030 } ;
3131 }
3232 } ,
33- { cleanProps : { } , restyleProps : { } , serialized : '' } as {
34- serialized : string ;
33+ { cleanProps : { } , restyleProps : { } , serializedRestyleProps : '' } as {
3534 cleanProps : TProps ;
3635 restyleProps : TRestyleProps ;
36+ serializedRestyleProps : string ;
3737 } ,
3838 ) ;
3939} ;
@@ -61,24 +61,30 @@ const useRestyle = <
6161) => {
6262 const theme = useTheme < Theme > ( ) ;
6363 const dimensions = useDimensions ( ) ;
64- const restylePropsRef = useRef < TProps > ( ) ;
6564
66- const { restyleProps , cleanProps , serialized } = filterRestyleProps (
65+ const { cleanProps , restyleProps , serializedRestyleProps } = filterRestyleProps (
6766 props ,
6867 composedRestyleFunction . propertiesMap ,
6968 ) ;
7069
71- restylePropsRef . current = restyleProps as TProps ;
72-
7370 const calculatedStyle = useMemo ( ( ) => {
74- const style = composedRestyleFunction . buildStyle ( restylePropsRef . current ! , {
71+ const style = composedRestyleFunction . buildStyle ( restyleProps as TProps , {
7572 theme,
7673 dimensions,
7774 } ) ;
7875
7976 return [ style , props . style ] . filter ( Boolean ) ;
77+ // We disable the exhaustive deps rule here in order to trigger the useMemo
78+ // when the serialized string of restyleProps changes instead of the object
79+ // reference which will change on every render.
8080 // eslint-disable-next-line react-hooks/exhaustive-deps
81- } , [ composedRestyleFunction , theme , dimensions , props . style , serialized ] ) ;
81+ } , [
82+ theme ,
83+ dimensions ,
84+ props . style ,
85+ serializedRestyleProps ,
86+ composedRestyleFunction ,
87+ ] ) ;
8288
8389 return {
8490 ...cleanProps ,
0 commit comments