简体   繁体   English

this.props.children 不包含子方法

[英]this.props.children doesn't contain child methods

import React from 'react';
import {render} from 'react-dom';

class Form extends React.Component {
    constructor(props) {
        super(props);
        this.validate = this.validate.bind(this);
    }

    componentDidMount() {

    }

    validate() {
        this.props.children.map((field)=> {
            field.validate();
        });
    }

    render() {
        return (
            <form className={this.props.className}>
                {this.props.children}
            </form>
        );
    }
}

export default Form;

Above is the Form.jsx上面是Form.jsx

import React from 'react';
import '../form.css';

class TextField extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            valid: true
        };
        this.validate = this.validate.bind(this);
    }

    validate() {
        if (!!this.props.required) {
            if (this.refs.field.value.trim().length === 0) {
                this.setState({
                    valid: false
                });
                return false;
            } else {
                this.setState({
                    valid: true
                });
                return true;
            }
        }
        return true;
    }

    setValue(event) {
        if (this.validate()) {
            this.props.setValue(this.props.name, event.target.value);
        }
    }

    render() {
        var input = (
            <span className={this.state.valid ? null : 'field-invalid'}>
                    <input ref="field" type="text" name={this.props.name} placeholder={this.props.placeholder}
                           onBlur={this.setValue.bind(this)}/>
            </span>
        );
        var field = input;
        if (this.props.label) {
            field = (
                <div className="row">
                    <label className={"col-3 align-r" + (!!this.props.required ? " field-required" : "")}>
                        {this.props.label}
                    </label>
                        <span className="col-6">
                            {input}
                        </span>
                </div>
            );
        }

        return field;
    }
}

export default TextField;

this is a field and contains validate method.这是一个字段,包含验证方法。 but this method is not accessible from Form.jsx this.props.children.但是这个方法不能从 Form.jsx this.props.children 访问。

Another parent Contains另一个父包含

<Form ref={(form)=> {this.form = form;}} className="label-input-group">
     <TextField label="Vehicle Owner" required={true} name="owner"/>
</Form>

validate function is undefined and throwing error.验证函数未定义并抛出错误。 this.props.children not updated in the parent after children mount i think. this.props.children 在我认为孩子登上后没有在父母中更新。 Any way to make it work?有什么办法让它工作吗?

I'm not sure it's a good idea, but you can do something like that :我不确定这是个好主意,但你可以这样做:

// Form
constructor(props) {
  super(props);
  this.validate = this.validate.bind(this);
  this.childrenInstances = [];
  this.props.children.forEach(field => {
    field.ref = inst => this.childrenInstances.push(inst);
  });
}

validate() {
  this.childrenInstances.forEach(field => {
    field.validate();
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM