Zustand question in next js
i have this code
and so, here i can add task and drag and drop, but if i delete and still works the same, why ?
export const useTaskStore = create<State & Actions>()(
persist(
set => ({
tasks: [],
draggedTask: null,
addTask: (title: string, description?: string) =>
set(state => ({
tasks: [
...state.tasks,
{ id: uuid(), title, description, status: 'TODO' }
]
})),
dragTask: (id: string | null) => set({ draggedTask: id }),
}),
{ name: 'task-store', skipHydration: true }
)
)
export const useTaskStore = create<State & Actions>()(
persist(
set => ({
tasks: [],
draggedTask: null,
addTask: (title: string, description?: string) =>
set(state => ({
tasks: [
...state.tasks,
{ id: uuid(), title, description, status: 'TODO' }
]
})),
dragTask: (id: string | null) => set({ draggedTask: id }),
}),
{ name: 'task-store', skipHydration: true }
)
)
export default function Column({
title,
status
}: {
title: string
status: Status
}) {
const tasks = useTaskStore(state => state.tasks)
const filteredTasks = useMemo(
() => tasks.filter(task => task.status === status),
[tasks, status]
)
const updateTask = useTaskStore(state => state.updateTask)
const dragTask = useTaskStore(state => state.dragTask)
const draggedTask = useTaskStore(state => state.draggedTask)
useEffect(() => {
useTaskStore.persist.rehydrate()
}, [])
const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
if (!draggedTask) return
updateTask(draggedTask, status)
dragTask(null)
}
return (
<section className='h-[600px] flex-1'>
<div
className='mt-3.5 h-full w-full rounded-xl bg-gray-700/50 p-4'
onDrop={handleDrop}
onDragOver={e => e.preventDefault()}
>
<div className='flex flex-col gap-4'>
{filteredTasks.map(task => (
<Task key={task.id} {...task} />
))}
{filteredTasks.length === 0 && status === 'TODO' && (
<div className='mt-8 text-center text-sm text-gray-500'>
<p>Create a new task</p>
</div>
)}
</div>
</div>
</section>
)
}
export default function Column({
title,
status
}: {
title: string
status: Status
}) {
const tasks = useTaskStore(state => state.tasks)
const filteredTasks = useMemo(
() => tasks.filter(task => task.status === status),
[tasks, status]
)
const updateTask = useTaskStore(state => state.updateTask)
const dragTask = useTaskStore(state => state.dragTask)
const draggedTask = useTaskStore(state => state.draggedTask)
useEffect(() => {
useTaskStore.persist.rehydrate()
}, [])
const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
if (!draggedTask) return
updateTask(draggedTask, status)
dragTask(null)
}
return (
<section className='h-[600px] flex-1'>
<div
className='mt-3.5 h-full w-full rounded-xl bg-gray-700/50 p-4'
onDrop={handleDrop}
onDragOver={e => e.preventDefault()}
>
<div className='flex flex-col gap-4'>
{filteredTasks.map(task => (
<Task key={task.id} {...task} />
))}
{filteredTasks.length === 0 && status === 'TODO' && (
<div className='mt-8 text-center text-sm text-gray-500'>
<p>Create a new task</p>
</div>
)}
</div>
</div>
</section>
)
}
useEffect(() => {
useTaskStore.persist.rehydrate()
}, [])
useEffect(() => {
useTaskStore.persist.rehydrate()
}, [])
skipHydration: true
skipHydration: true
2 Replies
FYI when you post blocks of code, it's helpful if you put
js
or ts
immediately after the opening ``` to enable syntax highlighting. Makes it much easier for anyone to parse what you're dealing withdone
thx
@πππΎππ (Solid fangirl) can u give me a hint for my pb?