jurassix / react-validation-mixin

Simple validation mixin (HoC) for React.
MIT License
283 stars 38 forks source link

Performing validation in componentWillMount() or componentDidMount() is not possible #63

Closed dminkovsky closed 8 years ago

dminkovsky commented 8 years ago

Motivation: validating props before the a component mounts so that errors are available on first render.


I tried:

componentWillMount() {
    this.props.validate()
}

but that failed because the validator component, a parent of my wrapped component, had not mounted yet so its refs was empty.

I've thought of some workarounds for this, but nothing really stuck out as great. Am I missing something?


I understand there's a bit of a chicken/egg problem here. Would it be possible to add a configuration to the top-level factory (the one that accepts the strategy) to make it perform this initial validation?

jurassix commented 8 years ago

Try adding it directly to the wrapped component.

Something like this:

const WrappedComponent = validation(strategy)(Component);
WrappedComponent.prototype.componentDidMount = function componentDidMount() {
  this.validate();
}
export default WrappedComponent;
dminkovsky commented 8 years ago

Duh of course. JavaScript! Thanks.

суббота, 19 декабря 2015 г. пользователь Clint Ayres написал:

Try adding it directly to the wrapped component.

Something like this:

const WrappedComponent = validation(strategy)(Component);WrappedComponent.prototype.componentDidMount = function componentDidMount() { this.validate(); }export default WrappedComponent;

— Reply to this email directly or view it on GitHub https://github.com/jurassix/react-validation-mixin/issues/63#issuecomment-166023642 .