Calculate Value going on an infinte loop when setting a value from API response
Hi, I am trying to assign API response to value using instance.setValue as suggested in this issue, https://github.com/formio/formio.js/issues/915 i am observing the code in the callback function is going in a infinite loop.
Note: Similar infinite loop behavior is observed in calculate value field while using instance.setValue(data) without any api call.
I feel instance.setValue is modifying the submission data object which triggers a change in the form and calls calculate value again by itself and hence the infinite loop.
How to make this work ?
Will you provide a jsfiddle of this happening? Or maybe the JSON of the form that has the problem.
I am facing this issue in form builder .
Steps:
Add a textfield and navigate to calculate value.The content of calculate Value filed is :
fetch('https://www.mocky.io/v2/5185415ba171ea3a00704eed')
.then(function(response) {
console.log('test');
instance.setValue(JSON.stringify(response));
});
I can observe the network calls and the console output is logging repeatedly.
@travist i dont observe this behavior if i use this json to render it in form renderer directly.
The calculate value would re-trigger on "setValue" so that is what is causing the infinite loop. Try adding a "changed" protection around your calculate value.
// Only set the value if it changes.
response = JSON.stringify(response);
if (instance.dataValue !== response) {
instance.setValue(response);
}