Last active
July 3, 2021 23:32
-
-
Save SunnyChopper/e73b163f005af400ba5ab672917aa8a1 to your computer and use it in GitHub Desktop.
Employee Container without `useMemo`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useEffect, useState } from 'react'; | |
| import { fetchEmployees } from '../api/employees'; | |
| const EmployeeSearchContainer = (props) => { | |
| const [selectedEmployee, setSelectedEmployee] = useState(null); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [searchText, setSearchText] = useState(''); | |
| const [employees, setEmployees] = useState([]); | |
| useEffect(() => { | |
| setIsLoading(true); | |
| fetchEmployees().then(employees => { | |
| // For the example, this returns 5k values | |
| setEmployees(employees); | |
| setIsLoading(false); | |
| }).catch(e => { | |
| setIsLoading(false); | |
| }); | |
| }, []); | |
| const filteredEmployees = () => { | |
| if (searchText && searchText.length > 0) { | |
| return employees.filter(employee => { | |
| if (employee.name.includes(searchText) || employee.email.includes(searchText)) { | |
| return employee.isActive; | |
| } else { | |
| return false; | |
| } | |
| }); | |
| } else { | |
| return employees; | |
| } | |
| } | |
| if (isLoading) { | |
| return 'Loading...'; | |
| } else { | |
| return ( | |
| <div> | |
| <input type="text" placeholder="Search for Employee" value={searchText} onChange={setSearchText} /> | |
| <EmployeesTable employees={filteredEmployees} onSelectEmployee={setSelectedEmployee} /> | |
| </div> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment