简体   繁体   English

在反应中传递状态或道具以将值传递给子组件

[英]Passing a state or prop to pass a value to child component in react

I'm quite new to react.我很新来反应。 I need to pass whether validation is success or not to child react component.Below is my parent component.我需要将验证是否成功传递给子反应组件。下面是我的父组件。

Login.js - Parent Login.js - 父级

update = name => {
    this.setState({inputValidation:false})// or with es6 this.setState({name})
  }

  nextClick = () => {
    const {type, nicPassportNumber, accountNumner } = this.state;
    if(type === ''){ //TODO add validations here
      alert('please enter a value to proceed');
      this.inputValidation = true;
      this.update();
      console.log("afetr : ", this.inputValidation);
      return;
    }
    const code = type === 'nic-pass' ? nicPassportNumber : accountNumner;
    this.props.verifyNumber(code, type);
  };

  render() {
    const {nicPassportNumber, accountNumner} = this.state;
    return (
      <div className="container-fluid">
              <div className="row form-group">
                  <div className = "col-lg-10 col-xl-6 offset-xl-3 offset-lg-1">
                      <Input_Box label = "Enter NIC / Passport" value={nicPassportNumber} onChangeFunc={this.handleChange} valid = {this.state.inputValidation} type='nic-pass' {...this.props}/>
                      <h2 className="sc-txt-hor-center my-3">OR</h2>
                      <Input_Box label = "Enter mobile / DTV / Broadband number" value={accountNumner} onChangeFunc={this.handleChange} valid = {this.state.inputValidation} type='account' {...this.props}/>
                  </div>
              </div>
              <Footer onBackClick={this.backClick} onNextClick={this.nextClick}/>
      </div>
    );

Input_Box.js - Child component Input_Box.js - 子组件

constructor(props) {
        super(props);
    }


    render() {
        const {label, value, onChangeFunc, type} = this.props;
        console.log("Val input box : ", this.props.inputValidation);

        return (
            <div className="sc-input">
                <label className="sc-input__label mb-3" htmlFor="nic_passport_no">{label}</label>
                <input type="text" 
                    className="form-control sc-input__box" 
                    id="nic_passport_no" 
                    placeholder="" 
                    value={value} 
                    onChange={(e) => onChangeFunc(e, type)  }/>
                <label className="sc-input__error-msg">123214</label>
            </div>
        );
    }
}

I tried most of the suggestion given in SO.我尝试了 SO 中给出的大部分建议。 But every time I'm getting undefined for inputValidation in child component.但是每次我在子组件中都undefined inputValidation时。

What I'm doing wrong here ?我在这里做错了什么?

This problem seems to be caused by incorrect props passed to <Input_Box/> :这个问题似乎是由传递给<Input_Box/>不正确道具引起的:

{/* use inputValidation prop rather than valid prop */}
<Input_Box inputValidation={this.state.inputValidation} label="Enter NIC / Passport" value={nicPassportNumber} onChangeFunc={this.handleChange} type='nic-pass' {...this.props}/>

<h2 className="sc-txt-hor-center my-3">OR</h2>

{/* use inputValidation prop rather than valid prop */}
<Input_Box inputValidation={this.state.inputValidation} label="Enter mobile / DTV / Broadband number" value={accountNumner} onChangeFunc={this.handleChange}  type='account' {...this.props}/>

Also, the reason console is logging undefined seems to be because you're accessing inputValidation from the component instance this rather than the state of the component:此外,控制台记录undefined的原因似乎是因为您从组件实例this访问inputValidation而不是组件的state

// console.log("afetr : ", this.inputValidation); 
console.log("after : ", this.state.inputValidation);

Hope this helps!希望这可以帮助!

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

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