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

add getUnhandledProps util #85

Merged
merged 1 commit into from
Oct 23, 2015
Merged

Conversation

levithomason
Copy link
Member

Fixes #69

This util gets all props for a component that are not defined in propTypes nor in defaultProps. These extra props are considered 'unhandled` by the component at hand.

The immediate usage of this util will be for spreading props the user passed in that we aren't already explicitly handling.

@eanplatter
Copy link
Contributor

This is an awesome utility? Could it be extended to work with extra props on classes? I guess it would be hard to tell if the classes are being used or not. Either way 🍂

it('leave props not in defaultProps || propTypes in tact', () => {
TestClass.defaultProps = {imHandled: 'thanks'};
TestClass.propTypes = {alsoHandled: 'got it'};
const props = {thisShould: 'still be here'};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick question here as to why you chose to make this variable a const as opposed to a let? Is it solely because this variable is meant to be read only? Or another, more specific reason?

😃 Just curious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! We've updated the linter. If you make a variable whose value never changes, it will tell you to make it a const instead. The only use case for a let now is for a variable whose value changes. Example:

let hasCertainChild;
React.Children.forEach(child => {
  if (someCondition(child) {
    hasSomeChild = true;
  }
})

In this case, the var is initialized as undefined and then set to true if the condition is met.

Going a bit further, a const wouldn't work here for both of those reasons. It cannot be initialized with an empty value nor can it's value change.

@athurman
Copy link
Contributor

🌊 🐙 Looks super handy

@levithomason
Copy link
Member Author

@eanplatter yes this util works with classes. Classes are just functions with a new name. The important thing is that the util looks for static properties SomeClass.propTypes and SomeClass.defaultProps to do it's magic. It finds these via this.constructor.<foo>.

These are equivalent and all produce objects like the above:

// ES5
function SomeClass() {}

// ES6
class SomeClass {}
SomeClass.propTypes = {};
SomeClass.defaultProps = {};

// ES7
class SomeClass {
  static propTypes = {};
  static defaultProps = {};
}

Finally, whether or not the class extends anything is irrelevant. Just so long as it has the right static props.

@levithomason levithomason reopened this Oct 23, 2015
levithomason added a commit that referenced this pull request Oct 23, 2015
@levithomason levithomason merged commit fd68111 into master Oct 23, 2015
@levithomason levithomason deleted the feature/unhandled-props-util branch October 23, 2015 15:46
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.

3 participants