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

How to handle propTypes undefined in production #7031

Closed
gajus opened this issue Jun 13, 2016 · 2 comments
Closed

How to handle propTypes undefined in production #7031

gajus opened this issue Jun 13, 2016 · 2 comments

Comments

@gajus
Copy link
Contributor

gajus commented Jun 13, 2016

I am aware that:

Note that for performance reasons propTypes is only checked in development mode

https://facebook.github.io/react/docs/reusable-components.html#prop-validation

In development mode, we have "models": collections of propTypes, e.g.

import { PropTypes } from 'react';
import Layout from './../../component/Layout';

export const schema = {
    direction: Layout.propTypes.direction.isRequired,
    components: PropTypes.arrayOf(Component)
};

Layout is React component. I am expecting to access propTypes.direction property of Layout component. This works in development environment. However, because propTypes are stripped in production, I am getting an error:

Uncaught (in promise) TypeError: Cannot read property 'direction' of undefined(…)

The only fix that I can think of in user land is adding a safe-check that eliminates the dead-code during the build stage, e.g.

export const schema = __DEV__ ? {
    direction: Layout.propTypes.direction.isRequired,
    components: PropTypes.arrayOf(Component)
} : null;

This is sub-optimal, because it would require updating each instance where propTypes are used to declare data shapes.

  • Is there an environment variable that would force to keep propTypes but disable validation?
  • Can you suggest an alternative approach to this issue?
@gaearon
Copy link
Collaborator

gaearon commented Jun 13, 2016

However, because propTypes are stripped in production, I am getting an error

PropTypes are not currently stripped in production. There was a proposal for this (#6401) but we haven’t agreed on it, and in any case, it’s not there in 0.14.x or 15.x.

React does not call your propTypes in production, but if you defined them, they will stay there. If propTypes declarations get stripped from your builds, it’s because you specifically choose to do this (e.g. via a Babel plugin). This is not the default behavior of React, so I’ll close this as this is unactionable for us.

@gajus
Copy link
Contributor Author

gajus commented Jun 13, 2016

Apologies. My blunder.

I forgot to account for the addition of https://github.com/thejameskyle/babel-react-optimize, which includes transform-react-remove-prop-types.

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

2 participants