简体   繁体   中英

Assigning form value from values within an array

I have a table that has an edit button next to each row. I send the persons id to the form component and get their data from the DB using the id. I assign it to patient. I then want to use the data to autofill a form.

Here is the top of the file.

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

    //console.log(this.props.location.search);
    const query = queryString.parse(this.props.location.search);
    console.log(query.patientId);

    this.state = {
      ptId: query.patientId || null,
      patient: null
    };

Here is the fetch that I confirm works when I log it.

  getPatient() {
    fetch("https://localhost:5001/api/PtEditPg/" + this.state.ptId)
      .then(response => response.json())
      .then(data => this.setState({ patient: data }));
  }

Here is the one of the forms showing what I have attempted

              <label htmlFor="firstName">First Name</label>
              <input
                type="Name"
                className="form-control"
                id="firstName"
                name="firstName"
                placeholder="First Name"
                value={this.state.patient.firstName}
                onChange={this.handleChange}
              />

I finally figured it out. The object was an array I think?

This is what worked

this.state.patient[0].firstName

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