I have a problem with returning data for an empty JSON. If there is data for a given user, everything works fine, unfortunately with empty JSON [] the data disappears and the else condition does not work. Does anyone know how to fix this?
const Header = (): ReactElement => {
const url = '/api/header';
const [ data, setData ] = useState([]);
useEffect(() => {
axios.get(url).then((json) => setData(json.data));
}, []);
return (
<Container maxWidth="xs" justify-content="center">
{data.map((collection) => {
if (data.length > 0) {
return (
<Header>
data1: {collection['data1']}
<br />
data2: {collection['data2']}
<br />
data3: {collection['data3']}
<br />
</Header>
);
} else
return (
<Header>
{' '}
data4: 0 <br /> data5: 0
</Header>
);
})}
</Container>
);
};
I'd like to return this:
return (
<StatisticsHeader>
{' '}
data4: 0 <br /> data5: 0
</Header>
);
in case of an empty JSON