simple diary : refactoring - useReducer
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import React, {
|
import React, {
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useReducer,
|
||||||
useEffect,
|
useEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
useCallback,
|
useCallback,
|
||||||
@@ -9,8 +9,37 @@ import "./App.css";
|
|||||||
import DiaryEditor from "./DiaryEditor";
|
import DiaryEditor from "./DiaryEditor";
|
||||||
import DiaryList from "./DiaryList";
|
import DiaryList from "./DiaryList";
|
||||||
|
|
||||||
|
const reducer = (state, action) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case "INIT": {
|
||||||
|
return action.data;
|
||||||
|
}
|
||||||
|
case "CREATE": {
|
||||||
|
const created_date = new Date().getTime();
|
||||||
|
const newItem = {
|
||||||
|
...action.data,
|
||||||
|
created_date,
|
||||||
|
};
|
||||||
|
return [newItem, ...state];
|
||||||
|
}
|
||||||
|
case "REMOVE": {
|
||||||
|
return state.filter((it) => it.id !== action.targetId);
|
||||||
|
}
|
||||||
|
case "EDIT": {
|
||||||
|
return state.map((it) =>
|
||||||
|
it.id === action.targetId ? { ...it, content: action.newContent } : it
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [data, setData] = useState([]);
|
// const [data, setData] = useState([]);
|
||||||
|
|
||||||
|
const [data, dispatch] = useReducer(reducer, []);
|
||||||
|
|
||||||
const dataId = useRef(0);
|
const dataId = useRef(0);
|
||||||
|
|
||||||
@@ -29,7 +58,7 @@ const App = () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
setData(initData);
|
dispatch({ type: "INIT", data: initData });
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -37,31 +66,27 @@ const App = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const onCreate = useCallback((author, content, emotion) => {
|
const onCreate = useCallback((author, content, emotion) => {
|
||||||
const created_date = new Date().getTime();
|
dispatch({
|
||||||
|
type: "CREATE",
|
||||||
|
data: {
|
||||||
|
author,
|
||||||
|
content,
|
||||||
|
emotion,
|
||||||
|
id: dataId.current,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const newItem = {
|
|
||||||
author,
|
|
||||||
content,
|
|
||||||
emotion,
|
|
||||||
created_date,
|
|
||||||
id: dataId.current,
|
|
||||||
};
|
|
||||||
dataId.current += 1;
|
dataId.current += 1;
|
||||||
|
|
||||||
setData((data) => [newItem, ...data]);
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const onRemove = useCallback((targetId) => {
|
const onRemove = useCallback((targetId) => {
|
||||||
alert(`${targetId}가 삭제되었습니다.`);
|
alert(`${targetId}가 삭제되었습니다.`);
|
||||||
setData((data) => data.filter((it) => it.id !== targetId));
|
|
||||||
|
dispatch({ type: "REMOVE", targetId });
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const onEdit = useCallback((targetId, newContent) => {
|
const onEdit = useCallback((targetId, newContent) => {
|
||||||
setData((data) =>
|
dispatch({ type: "EDIT", targetId, newContent });
|
||||||
data.map((it) =>
|
|
||||||
it.id === targetId ? { ...it, content: newContent } : it
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const getDiaryAnalysis = useMemo(() => {
|
const getDiaryAnalysis = useMemo(() => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useRef, useState, useEffect } from "react";
|
import React, { useRef, useState } from "react";
|
||||||
|
|
||||||
const DiaryItem = ({
|
const DiaryItem = ({
|
||||||
onEdit,
|
onEdit,
|
||||||
@@ -9,10 +9,6 @@ const DiaryItem = ({
|
|||||||
emotion,
|
emotion,
|
||||||
id,
|
id,
|
||||||
}) => {
|
}) => {
|
||||||
useEffect(() => {
|
|
||||||
console.log(`${id}번 째 아이템 렌더`);
|
|
||||||
});
|
|
||||||
|
|
||||||
const [isEdit, setIsEdit] = useState(false);
|
const [isEdit, setIsEdit] = useState(false);
|
||||||
const [localContent, setLocalContent] = useState(content);
|
const [localContent, setLocalContent] = useState(content);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user