简体   繁体   中英

React accessing children in form

I can not really access my dynamically generated children in a component. I found some cases on the internet, it seems like a common problem but nothing i could properly relate to or it was not really well documented so i got more confused.

I have a form which has a button and every time a press the button, subform is added to the form.

render: function() {
  var prop_forms = [];
  for(var i = 1; i <= this.state.count; i++){
    prop_forms.push(<App.Form.Property reference={i}/>);
  }
  return(
    <div>
      <h2>New model</h2>
      <form className="form" callback={this.submited}>
        <label>Name:</label>
        <input type="text" ref="name" className="fancy_input" required/>
        <label><input type="checkbox" ref="include_endpoints"/> Include basic endpoints</label>
        <h3>Properties <a onClick={this.add_property}><span className="glyphicon glyphicon-plus"></span></a></h3>
        {prop_forms}
        <button className="btn" onClick={this.submited} type="button">Add model to the canvas</button>
      </form>
    </div>
  );
}

every time a click a button, this.state.count is incremented so I get one more App.Form.Property component. I am passing the i variable so i can use it in my ref parameter in the App.Form.Property component.

render: function(){
  var input_ref = "property_"+this.props.reference+"_input";
  var select_ref = "property_"+this.props.reference+"_select";
  return(
    <div className="property_form">
      <input className="fancy_input" ref={input_ref} type="text" placeholder="Property name..."/>
      <select className="fancy_select" ref={select_ref}>
        <option value="string">String</option>
        <option value="integer">Integer</option>
        <option value="double">Double</option>
      </select>
    </div>
  );
}

My problem is that when i submit the form, I can not access the children via ref. I can only address name and include_endpoints but nothing from the App.Form.Property component.

在表单组件中添加一个函数getSubmitData,该函数封装来自外部元素的子代

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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