question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to control the`formData` in redux state

See original GitHub issue

Description

We get a formData object from the server that we allow the user to edit and we are using lodash ._set-object to update parts of that formData depending on what part of the formData the user updates.

Steps to Reproduce

We are trying to control the state of formData in redux state. We try to use componentWillReceiveProps, but can’t get the updated state back as a props. We put our action in onChange function and it changes the state correctly when we type in text. But when we finish typing it return back to empty string value as it can’t get updated state to the component as a props, and then the onChange function triggers the action once again because value of the input is empty.

We are trying to get the current value locally by setting the state inside the component but getting this error;

Uncaught Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops

This is our function;

onChange = ({ formData }) => {
const { path, editKeyword } = this.props;
const inputValue =formDataData[editKeyword];

let prevformData = this.props.formData;

// update the object using
const updatedFormData = _.set(prevFormData, path, inputValue);

// action
this.props.setCurrentFormData(updatedformData);};

We also tried the following with no luck;

onChange = ({ formData }) => {
const { path, editKeyword } = this.props;
const inputValue = formData[editKeyword];
console.log("input value ", inputValue);

let prevFormData = this.props.formData;

const updatedFormData = _.set(prevformData, path, inputValue);

// when we try to update local state
this.setState({ FormData: updatedformData})

};

Anyone encountered anything similar? We can’t directly use the formData from the form-input because we are merging values from server and the user input.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
LucianBuzzocommented, Nov 29, 2018

The issue is here https://github.com/mozilla-services/react-jsonschema-form/blob/master/src/utils.js#L190 - When a Form is updated, it checks computed formData against the formData prop, if there is a difference, then the onChange handler is called. If the formData value is falsey, then the this will result in an infinite loop, as the computed formData and props.formData will never be the same.

Update: I’ve opened a PR to fix this issue

1reaction
mikaelmhcommented, Dec 5, 2018

@LucianBuzzo thank you, this was great! We were able to make it work this past weekend. Awesome feedback.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux form is not storing input field values into redux state
Redux form by default stores the text under the form slice of the reducer, so your form data is under getState().form.addTeam .
Read more >
Values set in redux aren't applied to target input box #574
Currently react-redux-form input boxes are not populating the redux store form data as the value of the input box when mounted.
Read more >
Why Using HTML <form> Tag Caused Redux State to Reset
Imagine a form inside a React-Redux application. Every time the user fills one of the fields in the form, Redux state is changed....
Read more >
Getting Started - Redux Form
The store should know how to handle actions coming from the form components. To enable this, we need to pass the formReducer to...
Read more >
redux-form - npm
A higher order component decorator for forms using Redux and React. ... is to always try to use props instead of state when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found