simple diary - save data
This commit is contained in:
@@ -1,36 +1,56 @@
|
||||
import React, { useRef, useState } 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 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 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 (
|
||||
<div className="App">
|
||||
<DiaryEditor />
|
||||
<DiaryList diaryList={dummyList} />
|
||||
<DiaryEditor onCreate={onCreate} />
|
||||
<DiaryList diaryList={data} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useRef, useState } from "react";
|
||||
|
||||
const DiaryEditor = () => {
|
||||
const DiaryEditor = ({ onCreate }) => {
|
||||
const authorInput = useRef();
|
||||
const contentInput = useRef();
|
||||
|
||||
@@ -28,7 +28,13 @@ const DiaryEditor = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
onCreate(state.author, state.content, state.emotion);
|
||||
alert("저장 성공");
|
||||
setState({
|
||||
author: "",
|
||||
content: "",
|
||||
emotion: 5,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user