React get values ​​from dynamically added text input fields

I created dynamically generating text input fields, but could not find a way to read and receive values ​​and store them in an array. please find the code below

I have a separate component for adding new lines to an input field called IncrementTableRow

import React, {PropTypes} from 'react';

class IncrementTableRow extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <tr>
        <th scope="row">{this.props.index}</th>
        <td><input type="text" className="form-control" ref={"firstValue"+this.props.index} placeholder=""/></td>
        <td><input type="text" className="form-control" ref={"secondValue"+this.props.index} placeholder=""/></td>
      </tr>
    );
  }
}

export default IncrementTableRow;

In addition, I have the main component to call IncrementTableRow below - this is the calling line.

export default class SuggestInterestProductDetails extends Component {

constructor(props) {
    super(props);
    this.state = {
      rows: []
    };
    this.AddRow = this.AddRow.bind(this);
  }

AddRow() {
    this.setState({
      rows: [{val: 5}, ...this.state.rows]
    });
  }

  render() {
    let rows = this.state.rows.map(row => {
      return <Row />
    });

    return (
    <div>
      <button onClick={this.AddRow}>Add Row</button>
      <table>
        {rows}
      </table>
    </div>
    );
  }


}

I need to read all created values ​​of a text field and store it in an array

+4
source share
1 answer

- :

refs https://facebook.imtqy.com/react/docs/more-about-refs.html

let rows = this.state.rows.map(row => {
  return <Row />
});

, onChange

let rows = this.state.rows.map((row,i) => {
  return <Row ref={'row-'+i} onChange={(event) => this.myListener(event,i)} />
});
+1

All Articles