Skip to content

Apps that mount over isomorphically rendered forms aren't aware of early input #4293

Closed

Description

@d4goxn

Consider the following scenario (or run it), in a simple isomorphic app that consists of a single-input form.

  1. A form is rendered on the server with existing data and sent to the browser, along with dehydrated state
  2. The user edits the form
  3. The browser loads the app, slowly, which rehydrates its state and bootstraps over the form
  4. 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?

Activity

  1. d4goxn commented on Jul 5, 2015

    @d4goxn
    Author

    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...

  2. jimfb commented on Jul 7, 2015

    @jimfb
    Contributor

    @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.

  3. d4goxn commented on Jul 7, 2015

    @d4goxn
    Author

    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.

  4. dantman commented on Jul 27, 2015

    @dantman
    Contributor

    This seems to be the same issue as #2585.

  5. ericclemmons commented on Jul 31, 2015

    @ericclemmons
    Contributor

    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...

  6. DomBlack commented on Feb 21, 2017

    @DomBlack

    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.

  7. ericclemmons commented on Mar 9, 2017

    @ericclemmons
    Contributor

    I would have expected the onChange event to be triggered instead just after the DOM attaches.

    This would be a breaking change, but it makes sense to me.

    Is there a reason that this behavior is not correct?

  8. chrisblossom commented on Apr 27, 2017

    @chrisblossom

    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.

  9. chrisblossom commented on May 22, 2017

    @chrisblossom

    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

  10. stale commented on Jan 10, 2020

    @stale

    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.

  11. added
    Resolution: Stale
    on Jan 10, 2020
  12. stale commented on Jan 19, 2020

    @stale

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions