I’m stuck with a problem whereby I need to show only records that are active and hide inactive ones from a REST API call that I’m making.
If all records are inactive, just show only single message saying "No Records Found" for instance.
const [tenants, setTenants] = useState([]);
const tenant_status_active = 0;
<table className="table table-striped table-hover table-responsive caption-top">
<caption>Tenants Info:</caption>
<thead className="table-dark">
<tr>
<th>Room No.:</th>
<th>Name</th>
<th>E-mail Address</th>
<th>Is Deposit Paid?</th>
<th>Lease End Date</th>
<th colSpan={2}>Action</th>
</tr>
</thead>
<tbody>
{tenants.map(tenant => (tenant.tenantStatus !== tenant_status_active)
? (
<tr>
<td colSpan={7} className="text-danger">No Results</td>
</tr>
)
: (
<tr key={tenant.id}>
<td>{tenant.roomNumber}</td>
<td>{tenant.name} {tenant.surname}</td>
<td>{tenant.email}</td>
<td>{tenant.leaseEndDate}</td>
<td>
<button
aria-label="Delete tenant"
className="btn btn-danger"
onClick={() => deleteTenant(tenant)}
>
Delete
</button>
</td>
<td>
<button
aria-label="Update tenant"
className="btn btn-primary"
onClick={() => updateTenant(tenant)}
>
Update
</button>
</td>
</tr>
))
}
</tbody>
<tfoot className="table-dark">
<tr>
<td colSpan={7}><small>Table Data ©</small></td>
</tr>
</tfoot>
</table>
Tried to do a lot of google search for answers, also here on Stack overflow, and asked my instructor from Udemy for help. Unfortunately nothing is forthcoming at the moment.
2
Answers
You can filter out the inactive records and check active tenant array length:
Filter the
tenants
state for the tenants that have the correct active status value, and conditionally render either the fallback UI if that array is empty or the mapped JSX.Example: