ReactJS: How to get response array inside State(useState) and check the length of the array when rendering/binding. I dont know to set recentData array to the useState to get the length in the return.
import React, { useState } from "react";
import UseData from "../../hooks/useData";
const HomePage = (props) => {
const [data, setData] = useState(false);
!data &&
UseData(formattedData => {
const { hasError, recentData } = formattedData;
console.log(recentData) //// recentData has response
if (hasError) {
//
} else {
setData(recentData);
}
})
return (
// I want to get 'recentData' here like recentData.length > 0, have 'data' set true
{data && data.map((info, i) => (
<Grid key={i}>
member={info.firstName}
</Grid>
))}
// I want to get 'recentData' recentData.length === 1,
{data &&
<Grid key={i}>
member={info.firstName}
</Grid>
))}
// I want to get 'recentData' recentData.length === 0
<Grid >
No data to render
</Grid>
)
}
export default HomePage
recentData array returned from the useData hook
0:
{firstName: "XXXX"
lastName: "YYYY"}
1:
{firstName: "AAAA"
lastName: "BBBBB"}
2:
{firstName: "CCCC"
lastName: "DDDD"}
I dont know to set recentData array to the useState to get the length in the return.