You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Apps that mount over isomorphically rendered forms aren't aware of early input #4293
Consider the following scenario (or run it), in a simple isomorphic app that consists of a single-input form.
A form is rendered on the server with existing data and sent to the browser, along with dehydrated state
The user edits the form
The browser loads the app, slowly, which rehydrates its state and bootstraps over the form
The user hits a save button
At this point, React has found no reason to rerender the form or throw a warning, even though the values of the form elements have been updated by the user before the app was listening to them. As a result, app state is out of sync with form input values. The user has just re-saved their old data (app state), but sees their new data (form values). If they leave, they lose their changes even though it appears that they just successfully saved.
I have decided to solve the problem by putting a ref on every form input, and copying their values into app state on componentDidMount. I can probably make the solution a little more DRY than that sounds at first.
I'm thinking that a better solution might be to have React include input values in the checksums. Is there a technical reason why this is not how isomorphic rendering already works?
Wow, there is actually something really strange going on here. In src/renderers/dom/server, /ReactMarkupChecksum.js, ReactMarkupChecksum.canReuseMarkup seems to take a checksum of all of the existing markup before an app mounts on existing React rendered markup. That markup should include value attributes, and those value attrs should have been altered by the browser when the user edited their values; the checksum check should be failing in my repro, and I should be getting a warning or an invariant violation or something...
@d4goxn If the user is editing the form, the markup (attributes) remain unchanged while the javascript value (property) changes. This is why markup validation does not fail. Keep in mind that markup validation was intended as a sanity check for SSR, and was not intended to protect against this use case.
With regards to the race condition, yes, it does exist, but it's a little unclear what we could/should do about it. One option is to enqueue the events until such time as the required javascript has loaded, and then play the events - but this requires that the event listeners have been setup which requires that at least some minimal javascript has been loaded. We generally think of it in terms of a user clicking a menu which is a no-op until the javascript loads (interactions lost), but the form field modifications is an interesting case.
An option specific to form fields is that we could check in componentDidMount if the value property is equal to the value attribute, and if they are not equal, emit a set of events (is a single change event always sufficient?) such that they are made equal. Perhaps this is worth doing.
My best idea so far for solving this is to have any component that renders a form element, manually fire the onChange handlers for all inputs in the form, in it's componentDidMount method. That way, the lower level components that simply have inputs don't need to do anything special, and the higher level component that has to handle the problem doesn't need to know how the inputs map to props farther down the hierarchy.
I am a little concerned about the effect of spurious onChange calls, but as long as all of our logic is based on state and props, and not on events, then we should be ok.
I've had the same issue, but since my form inputs are controlled, it requires dispatching actions to update the models with the DOM values before React takes over & renders the page.
Of course, then there's an issue with the checksum as I've rendered a component that's now different than the server's version!
Yes, there is a race condition, but I'm not sure what the actual solution would be...
I've just encountered this race condition too with ControlledSelects, which results in the select change being reverted when the JS attaches to the existing DOM.
I would have expected the onChange event to be triggered instead just after the DOM attaches.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!
Activity
d4goxn commentedon Jul 5, 2015
FYI, I asked about how to do this properly on SO before I developed the opinion that this would best be solved inside of React. http://stackoverflow.com/questions/31227517/how-to-handle-early-input-to-isomorphically-rendered-forms/31230408#31230408
d4goxn commentedon Jul 5, 2015
Wow, there is actually something really strange going on here. In src/renderers/dom/server, /ReactMarkupChecksum.js,
ReactMarkupChecksum.canReuseMarkupseems to take a checksum of all of the existing markup before an app mounts on existing React rendered markup. That markup should include value attributes, and those value attrs should have been altered by the browser when the user edited their values; the checksum check should be failing in my repro, and I should be getting a warning or an invariant violation or something...jimfb commentedon Jul 7, 2015
@d4goxn If the user is editing the form, the markup (attributes) remain unchanged while the javascript value (property) changes. This is why markup validation does not fail. Keep in mind that markup validation was intended as a sanity check for SSR, and was not intended to protect against this use case.
With regards to the race condition, yes, it does exist, but it's a little unclear what we could/should do about it. One option is to enqueue the events until such time as the required javascript has loaded, and then play the events - but this requires that the event listeners have been setup which requires that at least some minimal javascript has been loaded. We generally think of it in terms of a user clicking a menu which is a no-op until the javascript loads (interactions lost), but the form field modifications is an interesting case.
An option specific to form fields is that we could check in componentDidMount if the value property is equal to the value attribute, and if they are not equal, emit a set of events (is a single change event always sufficient?) such that they are made equal. Perhaps this is worth doing.
d4goxn commentedon Jul 7, 2015
Ah, that makes sense.
My best idea so far for solving this is to have any component that renders a form element, manually fire the onChange handlers for all inputs in the form, in it's componentDidMount method. That way, the lower level components that simply have inputs don't need to do anything special, and the higher level component that has to handle the problem doesn't need to know how the inputs map to props farther down the hierarchy.
I am a little concerned about the effect of spurious onChange calls, but as long as all of our logic is based on state and props, and not on events, then we should be ok.
dantman commentedon Jul 27, 2015
This seems to be the same issue as #2585.
ericclemmons commentedon Jul 31, 2015
I've had the same issue, but since my form inputs are controlled, it requires dispatching actions to update the models with the DOM values before React takes over & renders the page.
Of course, then there's an issue with the checksum as I've rendered a component that's now different than the server's version!
Yes, there is a race condition, but I'm not sure what the actual solution would be...
DomBlack commentedon Feb 21, 2017
I've just encountered this race condition too with ControlledSelects, which results in the select change being reverted when the JS attaches to the existing DOM.
I would have expected the onChange event to be triggered instead just after the DOM attaches.
ericclemmons commentedon Mar 9, 2017
This would be a breaking change, but it makes sense to me.
Is there a reason that this behavior is not correct?
chrisblossom commentedon Apr 27, 2017
I created a test case that can be found here: chrisblossom@23bc06d.
It would be great to know if this is expected behavior or is a bug.
chrisblossom commentedon May 22, 2017
I created an input wrapper to solve this race condition. If anyone has any suggestions on how to fix any of the known issues please open a ticket/PR. https://github.com/chrisblossom/react-safe-universal-inputs
stale commentedon Jan 10, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.
stale commentedon Jan 19, 2020
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!