I'm using the next.js and i'm doing the fetch data fetch from json. Now i can't see the data from json (that change when i refresh) for example in the first box of the table.
Can someone help me? I just tried for so many days and i can't resolve.
import styles from '../styles2/Tabelle.module.css';
import { useEffect, useState } from 'react';
export default function Fetch() {
'use client';
const [appState, setAppState] = useState([]);
useEffect(() => {
const fetchData = async () => {
const response = await fetch('https://run.mocky.io/v3/f3bf41f3-c60a-47f8-9386-243989bead65');
const json = await response.json();
setAppState(json);
};
fetchData();
}, []);
const date = (
<ul>
{appState.map((item) => (
<li key={item.id}> {item.id} </li>
))}
</ul>
);
return (
<div>
<table>
<thead>
<tr>
<th>ID ESECUZIONE </th> <th>NOME PROCESSO</th> <th>HOST</th> <th>ANDAMENTO</th> <th>DURATA</th> <th>AVANZAMENTO</th>{' '}
</tr>
</thead>
<tbody>
<tr className={styles.righeEsecuzioni}>
<td> {date.key} </td>{' '}
</tr>
</tbody>
</table>
</div>
);
}
the response string does not contain array paranthesis [ ] in start and end thats why parsing JSON causing error
use axios library and you can do it like this
import library
fetch data
now add parenthesis at start and end
now setState
Overall code looks like this
Sort by Date
Using dayjs library
Add this after string variable