onvaluechanged -> setState -> infinite loop
See original GitHub issueAre you requesting a feature, reporting a bug or asking a question?
Question
What is the current behavior?
state grows exponentially each time i choose an option in my survey.
What is the expected behavior?
setState is called once every time an option is updated without interfering with the survey.
Test code
onValueChanged = (model) => (e) => {
fetch('http://localhost:5000/api/post', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(model.data)
}).then(json => {
this.setState({githubData: json});
}
);
};
Specify your
- browser: Firefox
- surveyjs platform: React
- surveyjs version: Latest
I am trying to call my api everytime a user selects an option or changes it. With that information i am trying to update a sidebar. But if i call setstate in onvaluechanged first the state grows exponentially and second the options don’t reset. So if normally i can choose one of three options, with setstate enabled i can choose all three. Any ideas?
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
ReactJS - Infinite loop, setState onChange method
There is an infinite loop because you call this.handleChange in your editor component when a change occurs in the CodeMirror instance.
Read more >How to Solve the Infinite Loop of React.useEffect()
useEffect() hook because it can generate infinite loops. ... const onChange = ({ target }) => setValue(target.value);. return (.
Read more >setState -> infinite loop · Issue #1308 · surveyjs/survey-library
setState is called once every time an option is updated without interfering with the survey. Test code. onValueChanged = (model) => (e) =>...
Read more >Why does my code cause an infinite loop and how do I fix ...
I am trying to delete a task from an array inside the parent's state so the child gets deleted when re-rendering but it...
Read more >Infinite loop when changing state from onChange-Reactjs
Coding example for the question React - Infinite loop when changing state from onChange-Reactjs.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@xadie yes you are right! @swilso793 We recommended to create model and add event listeners only once. For example:
could you please try this approach?
It’s not a React Issue. It’s an issue in Survey.js-react. The cause of this is a combination of missuse of the componentWillReceiveProps event in reactSurvey.tsx in combination with the updateSurvey function in reactSurvey.tsx
Instead of replacing all listeners on an Event on each updateSurvey-call they are added.
Whenever the react event componentWillReceiveProps is triggered on the Survey Component it will add all by prop provided event listeners on top on the old list over the lifetime of the component. Basically the function bleeds old-state values into new-state values.