-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Proposal: Validating object against propTypes object outside or render/context flows #6698
Comments
AFAIK PropTypes are considered legacy and are in maintenance mode. It's unlikely additional development will be happening on them. |
@zyrolasting Proptypes do (or will soon) become noop functions in production (to save bytes when downloading React in production mode), so they aren't usable in the production build. Also, as @aweary mentioned, proptypes are legacy, so we aren't adding new features. I think your best bet would be to copy-paste the validator code from React into a separate library, which you could then use in production. Or, like you mentioned, use a third party validation library. I'm going to close this out for the reasons above. Feel free to continue the discussion on this thread. |
Thanks for the prompt response, guys. I have nothing else to add given the circumstances. Have a great day! |
(sorry, not directly related, but this issue is the first time I've seen proptypes marked as legacy) |
You can keep using them if you like. They’re not going away soon. |
Oh, thanks, Dan. That makes total sense. I've been thinking about overlaps of PropTypes and TypeScript validations just recently. |
I tried using Flow in React 15 with create-react-app and flow type errors didn't show up in the create-react-app console, nor did they play nice with standard eslint so I had to abandon it. If we can that last bit of integration working for Flow, it'd be very nice to use. |
To avoid using lots of event handling and manual prop chaining inside a parent form, I wrote a component that accumulates form data using a context like so:
The context just exposes a function where any child (namely an input) reports a final JSON key, value and validation status. Although I could bring in a 3rd party schema validator, I wanted to see if I could leverage the
schema
property as an object containing React proptypes.I understand that proptypes don't get checked when rendering in production, but I was hoping that in
matchSchema
I want to call whatever React uses to take an object and check it against the accumulated form data.i.e.
From looking at the docs and StackOverflow, it looks like there is no exposed API to just validate against proptypes. The only solution that I can find involves using stateless components and intercepting console messages.
A paranoid solution might look like:
...But this is not going to fly because I don't want to run React in dev mode in production.
Can React expose a way to take advantage of its validation system regardless of its mode, so that I can test an object's shape and property types without hacking around the flow?
The text was updated successfully, but these errors were encountered: