function Inhook(props) {
const initialState = 0;
const [records, setRecords] = useState([]);
const [count, setCount] = useState(initialState);
const [show, setShow] = useState(false);
const [store, setStore] = useState(initialState);
const [store1, setStore1] = useState(initialState);
setRecords(props.records);
function val(e) {
debugger;
console.log("hhh", e);
setStore(e);
if (store === store1) {
if (count == 0) {
setShow(true);
} else {
setShow(false);
}
} else {
setShow(true);
setCount(0);
}
setCount(count + 1);
// console.log(count,'counttt');
setStore1(e);
}
return (
<div>
<React.Fragment>
<br />
<div className="Tree">
<h1 style={{ textAlign: "center" }}>Employee Tree</h1>
<h3>Test Case 1</h3>
<h4>Employee Tree</h4>
{
(records.sort((x, y) => (x.empName.toLowerCase() > y.empName.toLowerCase()) * 2 - 1),
records.map(empId => {
return empId.managerId === 0 ? (
<ul key={empId.id}>
<li style={{ fontWeight: "Bold" }}>
{empId.empName.toLowerCase()}
<p>Employees of : {empId.empName.toLowerCase()}</p>
<ul>
{records.map(s =>
s.managerId === empId.id ? (
<li type="square" key={s.id}>
{s.empName.toLowerCase()}
{empId.managerId === s.id ? <p>Employees of :{s.empName.toLowerCase()}</p> : <p></p>}
{records.map(e =>
e.managerId === s.id ? (
<div>
{e.managerId === s.id ? <p>{val(s.id)}</p> : <p></p>}
{show ? <p>Employees of :{s.empName.toLowerCase()}</p> : <p></p>}
<li key={e.id} type="disc" style={{ marginLeft: "120px" }}>
{e.empName.toLowerCase()}
</li>
</div>
) : (
""
)
)}
</li>
) : (
""
)
)}
</ul>
</li>
</ul>
) : (
""
);
}))
}
</div>
</React.Fragment>
</div>
);
}
export default Inhook;
I got data of records from json which it has name, id , managerId and salary and so on. when i did in class component it worked got the output ,in the function or hooks the val() function in the jsx calling infinity times got the error like (Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.)