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

Allow custom PropTypes to be chainable (isRequired) #1715

Closed
lettertwo opened this issue Jun 18, 2014 · 6 comments
Closed

Allow custom PropTypes to be chainable (isRequired) #1715

lettertwo opened this issue Jun 18, 2014 · 6 comments

Comments

@lettertwo
Copy link

A way to make a custom proptype required would be useful. Maybe some form of the createChainableTypeChecker factory could be exposed?

@chenglou
Copy link
Contributor

I'm wary of exposing an API for this. As you can see, it's really just quick sugar for checking whether the prop is undefined. Adding a props[propName] == null check to your custom validator does the same thing.

@lettertwo
Copy link
Author

Yeah, I think the main benefit of exposing something for this wouldn't be providing a way of making a prop required, but instead providing a way of having the option of making it required:

var MyPropType = chainablePropTypeFactory(function () {
 // prop type validation here.
});


propTypes: {
  prop: MyPropType,
  requiredProp: MyPropType.isRequired
}

So that a custom prop type can easily be created and used for both optional and required props.

@RR2DO2
Copy link

RR2DO2 commented Jul 22, 2014

I was about to file a similar issue, my main reason was to not have to duplicate the error handling of isRequired. This changed from 0.10 to 0.11 for example (not documented) and led to some issues in my code during the migration. Separately it is a nice UX if error messages stay consistent. My code looks something like this, which is really just duplication of some code in createChainableTypeChecker.checkType:

      myProp: function(props, propName, componentName) {
        if (props[propName] == null) {
          return new Error("Warning: Required prop `" + propName +
                           "` was not specified in `" +  componentName +
                           "`)");
        } else ...
      },

@sophiebits
Copy link
Collaborator

Also wary of exposing an API for this. In the long term you can use Flow or other systems and we'll probably move propTypes out of core eventually.

@angryobject
Copy link

It's old, but agreeing with what @lettertwo wrote, it could look like this until we get an exposed API:

const chainablePropType = predicate => {
  const propType = (props, propName, componentName) => {
    // don't do any validation if empty
    if (props[propName] == null) {
      return;
    }

    return predicate(props, propName, componentName);
  }

  propType.isRequired = (props, propName, componentName) => {
    // warn if empty
    if (props[propName] == null) {
      return new Error(`Required prop \`${propName}\` was not specified in \`${componentName}\`.`);
    }

    return predicate(props, propName, componentName);
  }

  return propType;
}

And later:

const customProp = chainablePropType(() => {...});
const propTypes = {
  prop: customProp,
  requiredProp: customProp.isRequired
};

In case this still might be helpful to somebody.

@charles-haynes
Copy link

It would also be useful so checkers like eslint could tell when default props were needed for a custom property.

vojtechsimetka added a commit to Giveth/giveth-dapp that referenced this issue Nov 17, 2017
… backup wallet button. Also added wallet model but it is currently unused as custom props don't allow chaining facebook/react#1715.
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

No branches or pull requests

6 participants