Constructor onChange breaks container components that sync state with onChange
See original GitHub issuePrerequisites
- I have read the documentation;
- In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.
- Ideally, I’m providing a sample JSFiddle or a shared playground link demonstrating the issue.
Description
https://github.com/mozilla-services/react-jsonschema-form/pull/1034 introduced a breaking change for a case of mine that involves an external container/wrapper component that has state including formData synchronized via the onChange handler. I haven’t updated my fork in awhile but in doing so I encountered this error across many instances of this container:
Warning: setState(…): Cannot update during an existing state transition (such as within
renderor another component’s constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved tocomponentWillMount
Example: https://codepen.io/pushred/pen/pYgJbq?editors=0011#0
Steps to Reproduce
- Mount container component with onChange wired to setState
- Observe console error
Expected behavior
I’m expecting onChange to be called only on changes, not on mount. I understand that this call is conditioned on a change between the initial react-jsonschema-form state and what’s passed as a prop. But for that to apply seems to require providing an initial data object that matches the schema / react-jsonschema initial state. An empty or undefined object doesn’t work.
I think this conflicts with some examples out there and definitely with my real world usage in creating new records or dealing with resources that provide incomplete data.
I will workaround this in my fork by simply removing the feature but wanted to document what I ran into. I think the root decision was incorrect as an external setState is a reasonable side effect of an event handler, especially onChange. I think componentDidMount should be reconsidered as the appropriate lifecycle method.
Actual behavior
💥
Version
1.2.1
Issue Analytics
- State:
- Created 5 years ago
- Reactions:12
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
That makes sense @pushred , thanks for the report. Is there a way to make onChange called only on changes, not on mount, but still provide a satisfactory solution for #1033?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you.