I'm fetching data from node.js. So it's kinda late to get data... I guess that's why my console says 'null' all the time :/ I tried several way to fix it, but it didn't help at all.
I did use UseState but it ended up saying 'null' data. My page kept saying
TypeError: Cannot read property 'map' of null
here's my react code:
const Start = () => {
const [isLoading, setIsLoading] = useState(false);
const dataList = null;
useEffect(() => {
const getData = async () => {
setIsLoading(true);
await axios
.post('/api/getData')
.then((res) => {
console.log(res.data.result);
// 👆 at this point, this console.log says what I exactly want.
return (dataList = res.data.result);
})
.catch((err) => console.log(err));
setIsLoading(false);
};
getData();
}, []);
if (isLoading) return <div>...Loading...</div>;
return (
<div>
{dataList.map((item, idx) => (
<p>{item[0].dataName}</p>
))}
</div>
);
};
What should I do to use my data which was fetched from node.js? the data is long, large JSON.