In my React code, I have an Employees page rendering both a Table component and a Filter component. The Filter code manipulates some data stored in the Employees page and then updates the Filter's state through useEffect. The data in question is passed as a prop to both the Filter component and the Table component. However, only the Filter component is re-rendering when this change happens. Excerpts of code below. Sample of filter function:
function filterFunction() {
let spliceArray = [];
if ($('#firstNameFilter').val() !== '') {
for (let i = 0; i < props.employees.length; i++) {
if(props.employees[i].firstName !== $('#firstNameFilter').val()) {
spliceArray.push(i);
}
}
for (let i = spliceArray.length - 1; i >= 0; i--) {
props.employees.splice(spliceArray[i], 1);
}
}
setUseFilter(true);
}
Sample of employees array on main page:
let employees = [
{
_id: 2,
firstName: 'Some',
lastName: 'One',
department: 'Somewhere',
jobTitle: 'Something',
email: '[email protected]',
phoneExtension: 67890
},
]
Sample of Table rendering code:
<tbody>
{props.employees.map((employee) => {
<tr>
<td><i id={employee._id + "upd"} className="fas fa-edit" onClick={handleEdit}></i></td>
<td><i id={employee._id + "del"} className="fas fa-trash-alt" onClick={deleteEmployee}></i></td>
<td>{employee.firstName}</td>
<td>{employee.lastName}</td>
<td>{employee.department}</td>
<td>{employee.jobTitle}</td>
<td>{employee.email}</td>
<td>{employee.phoneExtension}</td>
</tr>
</tbody>
So ideally, the filter would update the employees array and then the table would re-render with the updated array to reflect only the appropriate parts of the array per the filter. However, currently the array is being updated but the table is not re-rendering.
I am relatively new to React but my understanding was that a component would re-render whenever its state or props changed. Although I am unaware of a way to change the state of the Table component from within the Filter component, it seems like the change in the employees array prop should trigger it.
Any help is appreciated.
Per request, full Employee file below:
import React from 'react';
import Title from '../components/Title/Title.js';
import Table from '../components/Table/Table.js';
import Filter from '../components/Filter/Filter.js';
import { Container, Row, Col } from '../components/Grid/Grid.js';
import axios from 'axios';
let employees = [
{
_id: 2,
firstName: 'Some',
lastName: 'One',
department: 'Somewhere',
jobTitle: 'Something',
email: '[email protected]',
phoneExtension: 67890
},
{
_id: 3,
firstName: 'Test',
lastName: 'McTester',
department: 'Sales',
jobTitle: 'Seller',
email: '[email protected]',
phoneExtension: 13579
}];
let employeesControl = [
{
_id: 2,
firstName: 'Some',
lastName: 'One',
department: 'Somewhere',
jobTitle: 'Something',
email: '[email protected]',
phoneExtension: 67890
},
{
_id: 3,
firstName: 'Test',
lastName: 'McTester',
department: 'Sales',
jobTitle: 'Seller',
email: '[email protected]',
phoneExtension: 13579
}];
function Employees() {
return(
<Container fluid>
<Row>
<Col size="md-12">
<Title>Employee Directory</Title>
</Col>
</Row>
<Row>
<Col size="md-12">
<Filter employees={employees} employeesControl={employeesControl}/>
</Col>
</Row>
<Row>
<Col size="md-12">
<Table employees={employees} />
</Col>
</Row>
</Container>
);
}
export default Employees;
FilterandTablecomponent code, probably the issue there