1
- import { useContext , useState , useEffect , useRef } from 'react'
1
+ import { useContext , useState , useEffect , useMemo } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
import { cloneDeep } from 'lodash'
4
4
@@ -26,8 +26,12 @@ export const ConfigInput = ({ data, inputParam, disabled = false, arrayIndex = n
26
26
const [ expanded , setExpanded ] = useState ( false )
27
27
const [ selectedComponentNodeData , setSelectedComponentNodeData ] = useState ( { } )
28
28
29
- // Track the last processed input values to prevent infinite loops
30
- const lastProcessedInputsRef = useRef ( { } )
29
+ // Track the last processed input values to prevent infinite loops using useState
30
+ const [ lastProcessedInputs , setLastProcessedInputs ] = useState ( {
31
+ mainValue : null ,
32
+ configValue : null ,
33
+ arrayValue : null
34
+ } )
31
35
32
36
const handleAccordionChange = ( event , isExpanded ) => {
33
37
setExpanded ( isExpanded )
@@ -64,6 +68,18 @@ export const ConfigInput = ({ data, inputParam, disabled = false, arrayIndex = n
64
68
setSelectedComponentNodeData ( nodeData )
65
69
}
66
70
71
+ // Memoize current input values for reliable comparison
72
+ const currentInputValues = useMemo (
73
+ ( ) => ( {
74
+ mainValue : data . inputs [ inputParam . name ] ,
75
+ configValue : data . inputs [ `${ inputParam . name } Config` ] ,
76
+ arrayValue : parentParamForArray ? data . inputs [ parentParamForArray . name ] : null
77
+ } ) ,
78
+
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
80
+ [ data . inputs , inputParam . name , parentParamForArray ?. name ]
81
+ )
82
+
67
83
// Load initial component data when the component mounts
68
84
useEffect ( ( ) => {
69
85
const loadComponentData = async ( ) => {
@@ -138,11 +154,11 @@ export const ConfigInput = ({ data, inputParam, disabled = false, arrayIndex = n
138
154
setSelectedComponentNodeData ( componentNodeData )
139
155
140
156
// Store the processed inputs to track changes
141
- lastProcessedInputsRef . current = {
157
+ setLastProcessedInputs ( {
142
158
mainValue : data . inputs [ inputParam . name ] ,
143
159
configValue : data . inputs [ `${ inputParam . name } Config` ] ,
144
160
arrayValue : parentParamForArray ? data . inputs [ parentParamForArray . name ] : null
145
- }
161
+ } )
146
162
}
147
163
148
164
loadComponentData ( )
@@ -154,15 +170,10 @@ export const ConfigInput = ({ data, inputParam, disabled = false, arrayIndex = n
154
170
useEffect ( ( ) => {
155
171
if ( ! selectedComponentNodeData . inputParams ) return
156
172
157
- // Get current input values
158
- const currentMainValue = data . inputs [ inputParam . name ]
159
- const currentConfigValue = data . inputs [ `${ inputParam . name } Config` ]
160
- const currentArrayValue = parentParamForArray ? data . inputs [ parentParamForArray . name ] : null
161
-
162
- // Check if relevant inputs have changed
163
- const hasMainValueChanged = lastProcessedInputsRef . current . mainValue !== currentMainValue
164
- const hasConfigValueChanged = lastProcessedInputsRef . current . configValue !== currentConfigValue
165
- const hasArrayValueChanged = lastProcessedInputsRef . current . arrayValue !== currentArrayValue
173
+ // Check if relevant inputs have changed using strict equality comparison
174
+ const hasMainValueChanged = lastProcessedInputs . mainValue !== currentInputValues . mainValue
175
+ const hasConfigValueChanged = lastProcessedInputs . configValue !== currentInputValues . configValue
176
+ const hasArrayValueChanged = lastProcessedInputs . arrayValue !== currentInputValues . arrayValue
166
177
167
178
if ( ! hasMainValueChanged && ! hasConfigValueChanged && ! hasArrayValueChanged ) {
168
179
return // No relevant changes
@@ -224,17 +235,17 @@ export const ConfigInput = ({ data, inputParam, disabled = false, arrayIndex = n
224
235
setSelectedComponentNodeData ( updatedComponentData )
225
236
226
237
// Update the tracked values
227
- lastProcessedInputsRef . current = {
228
- mainValue : currentMainValue ,
229
- configValue : currentConfigValue ,
230
- arrayValue : currentArrayValue
231
- }
238
+ setLastProcessedInputs ( {
239
+ mainValue : currentInputValues . mainValue ,
240
+ configValue : currentInputValues . configValue ,
241
+ arrayValue : currentInputValues . arrayValue
242
+ } )
232
243
}
233
244
234
245
updateComponentData ( )
235
246
236
247
// eslint-disable-next-line react-hooks/exhaustive-deps
237
- } , [ data . inputs , inputParam . name , parentParamForArray ?. name , arrayIndex ] )
248
+ } , [ currentInputValues , selectedComponentNodeData . inputParams , inputParam . name , parentParamForArray ?. name , arrayIndex ] )
238
249
239
250
// Update node configuration when selected component data changes
240
251
useEffect ( ( ) => {
0 commit comments