Skip to content

Commit

Permalink
fix: zustand 예시 코드 변경, store 폴더 경로 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
banhogu committed May 14, 2024
1 parent d092f2e commit 26ae345
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
62 changes: 55 additions & 7 deletions src/store/example.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,68 @@ import { devtools, persist } from 'zustand/middleware';

//기본 사용법 타입 에러는 일단 무시

let userStore = (set) => ({
user: null,
setUser: (payload) =>
let todoStore = (set) => ({
todos: [],
addTodo: (payload) =>
set((state) => ({
user: payload.data
todos: [
...state.todos,
{
text: payload.text,
isCompleted: payload.isCompleted
}
]
})),

deleteTodo: (payload) =>
set((state) => ({
todos: state.todo.filter((todo) => todo.id !== payload.id)
})),

completeTodo: (payload) =>
set((state) => ({
todos: state.todos.map((todo) -> {
if(todo.id === payload.id){
return {
...todo,
isComplete : true
}
}
})
}))
});

// redux devtools처럼 chrome 확장자 앱 사용해서 사용가능
userStore = devtools(userStore);
todoStore = devtools(todoStore);

// persist 새로고침시 데이터 유지
userStore = persist(userStore, {
todoStore = persist(todoStore, {
name: 'userStore'
});

export const userStore = create(userStore);
//store 완성 다른 작업 필요없이 갖다쓰면됨
export const useTodoStore = create(todoStore);

// 예시
const {addTodo, deleteTodo ,completeTodo} = useTodoStore()

const handleAddTodo = () => {
addTodo{
text : 'Add Todo',
isCompleted : false,
id: 1
}
}

const handleDeleteTodo = () => {
deleteTodo{
id : 1
}
}

const handleCompleteTodo = () => {
completeTodo{
text : 'Add Todo',
isCompleted : false,
id: 1
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@types/*": ["src/types/*"],
"@utils/*": ["src/utils/*"],
"@constant/*": ["src/constant/*"],
"@models/*": ["src/models/*"]
"@models/*": ["src/models/*"],
"@store/*": ["src/store/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "next.config.mjs"],
Expand Down

0 comments on commit 26ae345

Please sign in to comment.