Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Props don't get updated for custom event handelrs #102

Closed
glennsl opened this issue Oct 23, 2017 · 2 comments
Closed

Props don't get updated for custom event handelrs #102

glennsl opened this issue Oct 23, 2017 · 2 comments

Comments

@glennsl
Copy link
Contributor

glennsl commented Oct 23, 2017

Given this (heavily reduced) component:

  let make ::count ::onChange _children => {

    let handleClick _ => {
      onChange (count + 1)
    };

    {
      ...component,

      didMount: fun self => {
        /* assume `element` has been acquired by legit means */
        element##addEventListener "click" handleClick;
        ReasonReact.NoUpdate
      },

      render: fun self =>
         ...
    }
  };

onChange will never be called with anything other than 1, regardless of what the count prop is changed to. The reason is of course that make will be called again on props change, which will define a new handleClick function that closes over the new prop value, but will not (and obviously should not) call didMount again to attach the updated event handler. So the old event handler with the old prop value will be called in perpetuity.

This isn't all that surprising if you understand how RR actually works, but it is if you expect it to work like reactjs' this.props (which of course I did, even though I should know better). RR creates the illusion, and thereby expectation, that it does work like this.props, until of course it doesn't. I suggest that this is made clear in the documentation somewhere, even if just as a FAQ.

There are two workarounds, neither of which are very nice:

  1. Copy the prop to state (or retainedProps?), and use state in the event handler instead.
  2. Remove and re-attach the event handler on didUpdate. This would also involve maintaining state to keep a reference of the currently attached event handler for removal, but is a more contained solution and good practice regardless of needing updated props, in case the element is re-inserted.

This seems like a fundamental restriction with the API as it currently is, but I wanted to bring it up so perhaps it can be addressed in some future iteration at least.

Full example here: https://github.com/glennsl/rr-stale-props-issue/blob/abb81215857d4240b5bfd748dbb28e4d98590d33/src/index.re

@chenglou
Copy link
Member

@cristianoc
Copy link
Contributor

My way of looking at things is that RR does not have props. It has state. And willReceiveProps really means will receive state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants