Skip to content

Mixins aren't available to custom propType validations #431

Closed
@tommyh

Description

@tommyh

In writing a custom propTypes validation function, I would like to be able to access mixins, for shared validation behavior.

Given the following mixin:

var MyMixin = {
    nameValidation: function(){
        console.log("won't be run...");
    }
};

My goal would be that I could do the following:

var Hello = React.createClass({
    mixins: [MyMixin],

    propTypes: {
        name: function(){
            this.nameValidation();            
        }
    },

    render: function() {
        return <div>Hello {this.props.name}</div>;
    }
});

But this fails because this is window, not the instance. Is it possible to autobind propType functions?

Here is a jsfiddle of the above code (slightly modified): http://jsfiddle.net/kXysA/1/

For reference: I also tried the following, but I'm not surprised this failed because this is undefined in this context:

var Hello = React.createClass({
    mixins: [MyMixin],

    propTypes: {
        name: this.nameValidation
    },

    render: function() {
        return <div>Hello {this.props.name}</div>;
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions