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

PropTypes in contextTypes and childContextTypes #77

Closed
insin opened this issue Jan 31, 2017 · 8 comments
Closed

PropTypes in contextTypes and childContextTypes #77

insin opened this issue Jan 31, 2017 · 8 comments

Comments

@insin
Copy link
Contributor

insin commented Jan 31, 2017

Could this transform also do something with contextTypes and childContextTypes?

They're the only other places PropTypes are used AFAIK, so handling them too would allow complete removal of PropTypes from an app's own production built code. This would also be useful for slimming down the production build of libraries which use context while still allowing PropTypes to be validated in development.

React doesn't care which values they have in production, but uses their property names to validate that only expected child context is provided and to mask context properties which are made available to a child, so they should be wrapped by default rather than removed (although you might still want to be able remove them if doing a cross-compatible build for something like Preact, which implements context without contextTypes and childContextTypes).

Example input and expected output:

class Foo1 extends React.Component {
  static contextTypes = {
    bar1: React.PropTypes.string,
  };

  render() {}
}
class Foo1 extends React.Component {

  render() {}
}

process.env.NODE_ENV !== "production" ? Foo1.contextTypes = {
  bar1: React.PropTypes.string
} : {
  bar1: 1
};
@oliviertassinari
Copy link
Owner

oliviertassinari commented Jan 31, 2017

@insin That sounds like a good idea.

I could only find one counter arguments.
You are not supposed to repeat contextTypes/childContextTypes all over the place. The API may change in the future. The React core team has put it in their priority for v16+. Hence, abstracting that in userspace with an Higher-order component is often preferable.

@insin
Copy link
Contributor Author

insin commented Jan 31, 2017

I agree, that's sound advice for end users, but library implementers currently have to use contextTypes/childContextTypes at some stage if their library uses context - this idea came while poking around in React Router v4's build, which is using this transform.

@insin
Copy link
Contributor Author

insin commented Feb 5, 2017

It would also be cool to have this transform available as a mode for propTypes for authors of libraries which need them at runtime in all environments for prop masking when passing props down, like React Bootstrap.

@oliviertassinari
Copy link
Owner

for prop masking when passing props down

I don't follow, what does that correspond to?

@lkmill
Copy link

lkmill commented Feb 10, 2017

+1, trying to build a server side rendering solution where i want to be able to drop in another module instead of react, and would be nice to be able to skip implementing all the prop types.

Ie, for my use case I will transform

import React from 'react';

// or

const React = require('react');

into

import React from 'jsx-node';

// or

const React = require('jsx-node');

which is quite easy to do, except for the prop types. Definately not impossible, but i would prefer not to.

I also use preact sometimes like OP mentioned.

@yasserkaddour
Copy link

yasserkaddour commented Apr 16, 2017

I have made a PR to deal with contextTypes and childContextTypes in #98, let me know if there is anything I overlooked.
Not there yet 😓

@oliviertassinari
Copy link
Owner

It's important to note that the context API is likely going to change: reactjs/rfcs#2.

@oliviertassinari
Copy link
Owner

The current context API has been deprecated. We close the issue :).

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

4 participants