Question T10118
Visible to All Users

Registering a custom function inside FunctionFactory throws error

created 2 years ago (modified 2 years ago)

Hi,

I added a method to calculate a date for some expression type fields like below:

JavaScript
export function addDays(params: any) {     if (!Array.isArray(params) || params.length !== 2 || !params[0]) return '';     const date = new Date(params[0]);     const daysToAdd = Number(params[1]);     const calculatedDate = dayjs(date)         .add(daysToAdd, 'days')         .format('MM/DD/YYYY');     return calculatedDate; }

However when I try to register it like below(im showing some other stuff registered as well), it throws a maximum update depth reached error:

JavaScript
SurveyCore.StylesManager.applyTheme('defaultV2'); SurveyReactUI.ReactQuestionFactory.Instance.registerQuestion(     'sv-tagbox-react',     (props: any) => {         return React.createElement(MultiSelect, props);     }, ); SurveyCore.RendererFactory.Instance.registerRenderer(     'checkbox',     'tagbox-react',     'sv-tagbox-react', ); //THIS IS THE LINE OF THE ERROR SurveyCore.FunctionFactory.Instance.register('addDays', addDays); // ... Survey component rendering code

Not sure why it is related to that since it works on a sandbox sample but not in my project.
Also when I have the same function addDays as exported from an outside file it throws some error but not when I have it on the same file.
Any guidance would be appreciated.

Answers approved by surveyjs Support

created 2 years ago

Hello,
It is very likely that addDays is undefined when you try to export it from another file.
Please debug your code and check the value of "addDays" export variable.

Thank you,
Andrew
SurveyJS Team

    Show previous comments (2)

      It works on sandbox but not on my project. The problem seems to be this code:

      JavaScript
      const onSurveyValueChanged = useCallback(     (sender: any) => {         const anyValueChanged = !deepCompare(             sender.data,             initialTemplateValues,         );         setTemplateValues(sender.data);         if (anyValueChanged !== isSurveyFormEdited) {             setIsSurveyFormEdited(anyValueChanged);         }     },     [initialTemplateValues, isSurveyFormEdited], );

      The setTemplateValues seems to be called infinitely on survey load, I suppose because that custom method gets triggered.

        Hello,

        I don't see how this code is related to your original question about custom function. If you still have the issue please provide us with a minimal live example illustrating the issue (https://stackoverflow.com/help/minimal-reproducible-example)).

        Thanks, Serge
        SurveyJS Team

          Well the custom method I am registering seems to fire the onSurveyValueChanged method infintely. But might be smth wrong with my code.