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

Deprecated PropType #14

Open
milesj opened this issue Mar 31, 2017 · 7 comments
Open

Deprecated PropType #14

milesj opened this issue Mar 31, 2017 · 7 comments

Comments

@milesj
Copy link

milesj commented Mar 31, 2017

A new PropType for denoting a prop as deprecated would be useful in slowly migrating components. It would simply call console.warn in non-production and pass through the original PropType.

Something like the following:

import { deprecated } from 'airbnb-prop-types';

const propTypes = {
  title: deprecated(PropTypes.string, 'Please use "children" instead.'),
  children: PropTypes.node,
};
@ljharb
Copy link
Owner

ljharb commented Mar 31, 2017

Since responsible apps make console warnings fail tests, that would cause deprecated propTypes to fail tests.

@milesj
Copy link
Author

milesj commented Mar 31, 2017

We could use console.info, or not log when NODE_ENV is "test". There are work-arounds to that issue.

@ljharb
Copy link
Owner

ljharb commented Mar 31, 2017

That's fair. console.info is probably better since NODE_ENV is rarely set to "test" in projects.

All of our propTypes have .isRequired options; what would a required + deprecated prop do?

@milesj
Copy link
Author

milesj commented Mar 31, 2017

Been thinking about that. The most I came up with is either a no-op, or it sets required on the 1st argument PropType.

@ljharb
Copy link
Owner

ljharb commented Apr 1, 2017

The latter makes the most sense to me - but the concept of a required deprecated propType might not make sense at all, given that if it's required, you can't migrate off of it.

@indexzero
Copy link

@ljharb PropTypes.deprecated would certainly be useful from my perspective, but as you mentioned PropTypes.deprecated.isRequired makes little sense. Most of the time I am deprecating a prop it is in favor of something else, which mutuallyExclusiveProps appears to help handle today.

e.g. consider a scenario where a Component currently has an onChange prop:

SomeComponent.propTypes = {
  onChange: PropTypes.func.isRequired
};

for various reasons it is decided to rename this prop to onValueChange (e.g. for consistency with other components prop names). In this case, as we roll-out the deprecation to be kind to our users in their upgrade timing we would want something like:

import { func } from 'prop-types';

const upgrade = mutuallyExclusiveProps(func, 'onChange', 'onValueChange');
SomeComponent.propTypes = {
  onValueChange: upgrade
  onChange: deprecated(upgrade)
};

Then once we've finally removed the deprecated property we can go back to the same as before:

SomeComponent.propTypes = {
  onValueChange: PropTypes.func.isRequired
};

@ljharb
Copy link
Owner

ljharb commented May 4, 2017

Sure, that's a reasonable pattern. However, I'd compose propTypes and make one of onValueChange or onChange required for the transition, otherwise you've created a window where neither might be passed.

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

No branches or pull requests

3 participants