simple diary - save data

This commit is contained in:
haerong22
2022-04-13 23:12:16 +09:00
parent 14535dcf47
commit 80db4a31d9
2 changed files with 52 additions and 26 deletions

View File

@@ -1,36 +1,56 @@
import React, { useRef, useState } from "react";
import "./App.css"; import "./App.css";
import DiaryEditor from "./DiaryEditor"; import DiaryEditor from "./DiaryEditor";
import DiaryList from "./DiaryList"; import DiaryList from "./DiaryList";
const dummyList = [ // const dummyList = [
{ // {
id: 1, // id: 1,
author: "kim", // author: "kim",
content: "hihi", // content: "hihi",
emotion: 5, // emotion: 5,
created_date: new Date().getTime(), // created_date: new Date().getTime(),
}, // },
{ // {
id: 2, // id: 2,
author: "lee", // author: "lee",
content: "hihi", // content: "hihi",
emotion: 2, // emotion: 2,
created_date: new Date().getTime(), // created_date: new Date().getTime(),
}, // },
{ // {
id: 3, // id: 3,
author: "park", // author: "park",
content: "hihi", // content: "hihi",
emotion: 4, // emotion: 4,
created_date: new Date().getTime(), // created_date: new Date().getTime(),
}, // },
]; // ];
const App = () => { const App = () => {
const [data, setData] = useState([]);
const dataId = useRef(0);
const onCreate = (author, content, emotion) => {
const created_date = new Date().getTime();
const newItem = {
author,
content,
emotion,
created_date,
id: dataId.current,
};
dataId.current += 1;
setData([newItem, ...data]);
};
return ( return (
<div className="App"> <div className="App">
<DiaryEditor /> <DiaryEditor onCreate={onCreate} />
<DiaryList diaryList={dummyList} /> <DiaryList diaryList={data} />
</div> </div>
); );
}; };

View File

@@ -1,6 +1,6 @@
import React, { useRef, useState } from "react"; import React, { useRef, useState } from "react";
const DiaryEditor = () => { const DiaryEditor = ({ onCreate }) => {
const authorInput = useRef(); const authorInput = useRef();
const contentInput = useRef(); const contentInput = useRef();
@@ -28,7 +28,13 @@ const DiaryEditor = () => {
return; return;
} }
onCreate(state.author, state.content, state.emotion);
alert("저장 성공"); alert("저장 성공");
setState({
author: "",
content: "",
emotion: 5,
});
}; };
return ( return (