simple diary : init data

This commit is contained in:
haerong22
2022-04-15 15:21:19 +09:00
parent e44b2cb702
commit 035cd6cd7e

View File

@@ -1,37 +1,35 @@
import React, { useRef, useState } from "react";
import React, { useRef, useState, useEffect } from "react";
import "./App.css";
import DiaryEditor from "./DiaryEditor";
import DiaryList from "./DiaryList";
// const dummyList = [
// {
// id: 1,
// author: "kim",
// content: "hihi",
// emotion: 5,
// created_date: new Date().getTime(),
// },
// {
// id: 2,
// author: "lee",
// content: "hihi",
// emotion: 2,
// created_date: new Date().getTime(),
// },
// {
// id: 3,
// author: "park",
// content: "hihi",
// emotion: 4,
// created_date: new Date().getTime(),
// },
// ];
const App = () => {
const [data, setData] = useState([]);
const dataId = useRef(0);
const getData = async () => {
const res = await fetch(
"https://jsonplaceholder.typicode.com/comments"
).then((res) => res.json());
const initData = res.slice(0, 20).map((it) => {
return {
author: it.email,
content: it.body,
emotion: Math.floor(Math.random() * 5) + 1,
created_date: new Date().getTime(),
id: dataId.current++,
};
});
setData(initData);
};
useEffect(() => {
getData();
}, []);
const onCreate = (author, content, emotion) => {
const created_date = new Date().getTime();