FabricioG
FabricioG

Reputation: 3320

React method doesn't return table data to display

In React I have a ternary operator returning a component if a condition is met:

              { this.state.houseHoldGroup.length > 0 ? (
              <Table className="align-items-center table-flush" responsive>
                <thead className="thead-light">
                  <tr>
                    <th scope="col">First</th>
                    <th scope="col">Last</th>
                    <th scope="col"></th>
                    <th scope="col"></th>
                  </tr>
                </thead>
                <tbody>
                {this.checkHouseholdGroup()}
                </tbody>
              </Table>
            ) : null }

Works good. Inside this component I have a method: this.checkHouseholdGroup()

The expected behavior is for this method to return the table data inside <tbody></tbody>

checkHouseholdGroup = () => {
  const householdDetails = this.state.houseHoldGroup;
  householdDetails.forEach(el => {
    console.log(el.firstHousehold)
    return (
      <tr>
        <th scope="row">{el.firstHousehold}</th>
        <td>{el.lastHousehold}</td>
        <td>
        <Button
          color="primary"
          href="#pablo"
          onClick={e => e.preventDefault()}
          size="sm"
          onClick={e => this.submitMember(e)}>
          Update
        </Button>
        </td>
        <td>
        <Button
          color="primary"
          href="#pablo"
          onClick={e => e.preventDefault()}
          size="sm"
          onClick={e => this.submitMember(e)}>
          Delete
        </Button>
        </td>
      </tr>
    )
  })
}

I can confirm the element has data. I console.log(el.firstHousehold) can see it's not empty. What am I doing wrong? The expected result would be that it would return my with the data in it.

Upvotes: 0

Views: 48

Answers (2)

lfarroco
lfarroco

Reputation: 624

Replace householdDetails.forEach with return householdDetails.map and you should be good.

forEach is used to create side effects - it does not return anything. The parent component of checkHouseholdGroup waits for a value to be returned, but nothing comes out of the function. Using return inside a forEach call will make the returned values go "nowhere". That's why you need to use map (ir returns a list with the elements), and then return the array.

Upvotes: 0

cullanrocks
cullanrocks

Reputation: 497

Have you tried mapping instead of using forEach?

checkHouseholdGroup = () => {
  const householdDetails = this.state.houseHoldGroup;
  return householdDetails.map(el => {
    console.log(el.firstHousehold)
    return (
      <tr>
        <th scope="row">{el.firstHousehold}</th>
        <td>{el.lastHousehold}</td>
        <td>
        <Button
          color="primary"
          href="#pablo"
          onClick={e => e.preventDefault()}
          size="sm"
          onClick={e => this.submitMember(e)}>
          Update
        </Button>
        </td>
        <td>
        <Button
          color="primary"
          href="#pablo"
          onClick={e => e.preventDefault()}
          size="sm"
          onClick={e => this.submitMember(e)}>
          Delete
        </Button>
        </td>
      </tr>
    )
  })
}

Upvotes: 1

Related Questions