Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"react-redux": "8.1.1",
"react-scripts": "5.0.1",
"styled-components": "6.0.5",
"web-vitals": "^2.1.0"
"web-vitals": "^2.1.0",
"zustand": "4.3.9"
},
"scripts": {
"start": "react-scripts start",
Expand Down
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TodoList from "./components/TodoList";
const App = () => {
return (
<Layout>
<AppTitle>리덕스로 만드는 투두리스트</AppTitle>
<AppTitle>zustand로 만드는 투두리스트</AppTitle>
<TodoInput />
<TodoList />
</Layout>
Expand Down
19 changes: 8 additions & 11 deletions src/components/TodoInput.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import React, { useState } from "react";
import { styled } from "styled-components";
import { useDispatch } from "react-redux";
import { addTodo } from "../redux/modules/todoSlice";
import { useTodoStore } from "../store/useTodoStore";

const TodoInput = () => {
const [title, setTitle] = useState("");
const [content, setContent] = useState("");

const dispatch = useDispatch();
const { addTodo } = useTodoStore((state) => state);

const handleClick = () => {
dispatch(
addTodo({
id: Date.now(),
title,
content,
isDone: false,
})
);
addTodo({
id: Date.now(),
title,
content,
isDone: false,
});
};

return (
Expand Down
9 changes: 4 additions & 5 deletions src/components/TodoItem.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import React from "react";
import { styled } from "styled-components";
import { useDispatch } from "react-redux";
import { deleteTodo, updateTodo } from "../redux/modules/todoSlice";
import { useTodoStore } from "../store/useTodoStore";

const TodoItem = (props) => {
const { todo } = props;
const { id, title, content, isDone } = todo;

const dispatch = useDispatch();
const { deleteTodo, updateTodo } = useTodoStore((state) => state);

const deleteTodoItem = (id) => {
dispatch(deleteTodo(id));
deleteTodo(id);
};

const updateTodoItem = (id) => {
dispatch(updateTodo(id));
updateTodo(id);
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/TodoList.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import { styled } from "styled-components";
import TodoItem from "./TodoItem";
import { useSelector } from "react-redux";
import { useTodoStore } from "../store/useTodoStore";

const TodoList = () => {
const { todos } = useSelector((state) => state.todoReducer);
const todos = useTodoStore((state) => state.todos);

return (
<TodoListContainer>
Expand Down
10 changes: 3 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { Provider } from "react-redux";
import { store } from "./redux/config/configStore";

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>
<React.StrictMode>
<App />
</React.StrictMode>
);
9 changes: 0 additions & 9 deletions src/redux/config/configStore.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/redux/modules/todoSlice.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/shared/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React from "react";
import React, { useEffect } from "react";
import GlobalStyle from "../styles/GlobalStyle";
import { styled } from "styled-components";
import { fetchTodos } from "../store/useTodoStore";

const Layout = ({ children }) => {
useEffect(() => {
fetchTodos();
}, []);

return (
<PageLayout>
<GlobalStyle />
Expand Down
28 changes: 28 additions & 0 deletions src/store/useTodoStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { create } from "zustand";

export const useTodoStore = create((set) => ({
todos: [],
setTodos: (todos) => set({ todos }),
addTodo: (newTodo) => set((state) => ({ todos: [...state.todos, newTodo] })),
updateTodo: (id) =>
set((state) => ({
todos: state.todos.map((todo) => {
if (todo.id === id) {
return { ...todo, isDone: !todo.isDone };
}
return todo;
}),
})),
deleteTodo: (id) =>
set((state) => ({ todos: state.todos.filter((todo) => todo.id !== id) })),
}));

export const fetchTodos = async () => {
const response = await fetch(
"https://jsonplaceholder.typicode.com/todos?_limit=3"
);
const todos = await response.json();
useTodoStore
.getState()
.setTodos(todos.map((todo) => ({ ...todo, isDone: false })));
};
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9083,7 +9083,7 @@ url-parse@^1.5.3:
querystringify "^2.1.1"
requires-port "^1.0.0"

use-sync-external-store@^1.0.0:
use-sync-external-store@1.2.0, use-sync-external-store@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a"
integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==
Expand Down Expand Up @@ -9649,3 +9649,10 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zustand@4.3.9:
version "4.3.9"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-4.3.9.tgz#a7d4332bbd75dfd25c6848180b3df1407217f2ad"
integrity sha512-Tat5r8jOMG1Vcsj8uldMyqYKC5IZvQif8zetmLHs9WoZlntTHmIoNM8TpLRY31ExncuUvUOXehd0kvahkuHjDw==
dependencies:
use-sync-external-store "1.2.0"