surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.11k stars 801 forks source link

Is there anything like onTyping or something? I'm using onValueChanged and I need some alternative #8062

Closed Priyeshvaghela77 closed 3 months ago

Priyeshvaghela77 commented 5 months ago

I need help. We are using the onValueChanged currently but in our react app when user is typing the data gets cleared out due to re-renders. I cant get the data they entered because the only way for me to get and set that data in any state is using onValueChanged or onValueChanging which is not very useful in this.

The data the user is entering will get cleared and since user has not clicked outside or lose focus the data will not be set to the state.

Please, help! What should I do?

JaneSjs commented 5 months ago

Hello @Priyeshvaghela77,

when user is typing the data gets cleared out due to re-renders

You may consider storing a survey model within a state variable to prevent re-rendering when a component state is changed.

function SurveyComponent() {
  const [surveyModel, setSurveyModel] = useState(null);
  ...
}

Please try storing a survey model within the state variable and let me know if this resolves the issue.

Priyeshvaghela77 commented 5 months ago

Hello @JaneSjs,

We are already using a state for that.

This is what we are doing (sample code):

import { useEffect, useRef, useState } from "react";
import { Model } from "survey-core";
import { Survey } from "survey-react-ui";
import "survey-core/defaultV2.min.css";
import * as SurveyCore from "survey-core";

export function LoadDynamicForm({ template }: { template: string }) {
  const [survey, setSurvey] = useState<Model | null>(null);

  useEffect(() => {
    const surveyModel = new Model(JSON.parse(template));
    setSurvey(surveyModel);
  }, [template]);

  return <>{survey && <Survey model={survey} />}</>;
}

package.json file:

    "react": "^18.2.0",
    "survey-creator-react": "^1.9.122",
    "survey-react": "^1.9.122",

In this, we are using the survey object to get the questions, set some default values, populate based on some conditions etc.

For, first few seconds when the form is opened, when user writes anything it gets cleared out due to state re-renders.

I also tried using redux state for this. But the issue still persists with redux also

Priyeshvaghela77 commented 5 months ago

@JaneSjs

Can you please tell me how/when the surveyjs stores the data user entered? As far as I observed it is done onBlur (when the input loses the focus)

I tried logging the survey.data but it's always cleared when the webapp is re-rendering

JaneSjs commented 3 months ago

Hello, User responses are stored within survey.data. An answer is passed to survey.data when a question value is submitted; it usually happens when an input loses the focus. I may recommend that you store user responses in some storage and load data when a survey component is being loaded. An example on how to store data in a browser local storage is available at Continue an Incomplete Survey.

Let us know if you have further questions.