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

Throw an error when functions on statics clash due to duplicate keys #2036

Merged
merged 2 commits into from
Aug 27, 2014
Merged

Throw an error when functions on statics clash due to duplicate keys #2036

merged 2 commits into from
Aug 27, 2014

Conversation

charliermarsh
Copy link

Fixes #1947.

I agree with @spicyj and find this behavior confusing, so I thought I'd send in a pull request. (This is my first PR for React--did my best to follow the contributing guidelines!)

@sophiebits
Copy link
Collaborator

I like this.

@charliermarsh
Copy link
Author

One thing I've noticed: Before this change, if you had a single mixin with its own getDefaultProps on statics, then the result of that call would be merged with the getDefaultProps of the base component. If you had two mixins that both defined their own getDefaultProps on their statics, then those functions would be chained, returning undefined, and so neither of them would have any influence on the base component's getDefaultProps.

After this change, the single mixin behavior remains the same. But if you have two mixins with their own getDefaultProps on statics, you get an error (which is probably more helpful than returning undefined, as they were before). Generally, do we want to be merging the result of each mixin's getDefaultProps?

@sophiebits
Copy link
Collaborator

I know I told you that getDefaultProps in statics should work but now that I'm looking again, I'm not sure if that was actually intended:

  /**
   * Special case getDefaultProps which should move into statics but requires
   * automatic merging.
   */

@charliermarsh
Copy link
Author

There's also a note above RESERVED_SPEC_KEYS:

/**
 * ...
 * Despite being static, they must be defined outside of the "statics" key
 * under which all other static methods are defined.
 */

Suggests to me that a getDefaultProps method defined on statics shouldn't be merged in.

@charliermarsh
Copy link
Author

As far as I can tell, (post-change,) putting statics in getDefaultProps only works because statics happens to get mixed into the spec before getDefaultProps. We could either force a certain order of iteration or (my preference) fail in mixStaticSpecIntoComponent if they try to add a key from RESERVED_SPEC_KEYS.

@sophiebits
Copy link
Collaborator

For now, maybe it's best to throw an error if getDefaultProps is in statics.

@charliermarsh
Copy link
Author

(Somewhat relevant: #1601. In this case, we'd want to make sure that the RESERVED_SPEC_KEYS get mixed in before any mixins. I still prefer throwing an error when you use a RESERVED_SPEC_KEY in statics, though.)

@sophiebits
Copy link
Collaborator

Although you'll probably be very confused if you use componentDidMount, etc. as static properties, I don't think there's a real reason to disallow it – I think getDefaultProps is the only problematic one because that gets stored on the constructor.

@charliermarsh
Copy link
Author

@spicyj Don't all the properties in RESERVED_SPEC_KEY get stored on the constructor? (componentDidMount is in ReactCompositeComponentInterface but not RESERVED_SPEC_KEY).

@sophiebits
Copy link
Collaborator

Yes, I'm an idiot. Carry on.

chenglou added a commit that referenced this pull request Aug 27, 2014
Throw an error when functions on `statics` clash due to duplicate keys
@chenglou chenglou merged commit 95de877 into facebook:master Aug 27, 2014
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

Successfully merging this pull request may close these issues.

Functions on statics get chained with duplicate keys?
3 participants