@@ -69,35 +69,19 @@ const DigitalFlop = ({ config = {}, className, style }) => {
6969 const rendererRef = useRef ( null )
7070 const graphRef = useRef ( null )
7171
72- const mergedConfigRef = useRef ( null )
73-
74- const mountedRef = useRef ( true )
75-
76- function init ( ) {
77- rendererRef . current = new CRender ( domRef . current )
78-
79- mergedConfigRef . current = getMergedConfig ( )
80-
81- graphRef . current = getGraph ( )
82- }
83-
84- const getMergedConfig = ( ) =>
85- deepMerge ( deepClone ( defaultConfig , true ) , config || { } )
86-
87- function getGraph ( ) {
88- const { animationCurve, animationFrame } = mergedConfigRef . current
72+ function getGraph ( mergedConfig ) {
73+ const { animationCurve, animationFrame } = mergedConfig
8974
9075 return rendererRef . current . add ( {
9176 name : 'numberText' ,
9277 animationCurve,
9378 animationFrame,
94- shape : getShape ( ) ,
95- style : getStyle ( )
79+ shape : getShape ( mergedConfig ) ,
80+ style : getStyle ( mergedConfig )
9681 } )
9782 }
9883
99- function getShape ( ) {
100- const { number, content, toFixed, textAlign } = mergedConfigRef . current
84+ function getShape ( { number, content, toFixed, textAlign } ) {
10185 const [ w , h ] = rendererRef . current . area
10286
10387 const position = [ w / 2 , h / 2 ]
@@ -108,22 +92,25 @@ const DigitalFlop = ({ config = {}, className, style }) => {
10892 return { number, content, toFixed, position }
10993 }
11094
111- function getStyle ( ) {
112- const { style, textAlign } = mergedConfigRef . current
113-
95+ function getStyle ( { style, textAlign } ) {
11496 return deepMerge ( style , {
11597 textAlign,
11698 textBaseline : 'middle'
11799 } )
118100 }
119101
120- useEffect ( init , [ ] )
102+ useEffect ( ( ) => {
103+ const mergedConfig = deepMerge ( deepClone ( defaultConfig , true ) , config || { } )
104+
105+ if ( ! rendererRef . current ) {
106+ rendererRef . current = new CRender ( domRef . current )
107+
108+ graphRef . current = getGraph ( mergedConfig )
109+ }
121110
122- function update ( ) {
123- const mergedConfig = ( mergedConfigRef . current = getMergedConfig ( ) )
124111 const graph = graphRef . current
125112
126- const shape = getShape ( )
113+ const shape = getShape ( mergedConfig )
127114
128115 const cacheNum = graph . shape . number . length
129116 const shapeNum = shape . number . length
@@ -134,16 +121,8 @@ const DigitalFlop = ({ config = {}, className, style }) => {
134121
135122 Object . assign ( graph , { animationCurve, animationFrame } )
136123
137- graph . animation ( 'style' , getStyle ( ) , true )
124+ graph . animation ( 'style' , getStyle ( mergedConfig ) , true )
138125 graph . animation ( 'shape' , shape )
139- }
140-
141- useEffect ( ( ) => {
142- ! mountedRef . current && update ( )
143-
144- return ( ) => {
145- mountedRef . current = false
146- }
147126 } , [ config ] )
148127
149128 const classNames = useMemo ( ( ) => classnames ( 'dv-digital-flop' , className ) , [
0 commit comments