diff --git a/examples/task-list/server/tasks.js b/examples/task-list/server/tasks.js index 756512c..ef28e0c 100644 --- a/examples/task-list/server/tasks.js +++ b/examples/task-list/server/tasks.js @@ -47,7 +47,13 @@ export const updateTask = (id, updates) => { if (!task) return undefined; - Object.assign(task, updates, { lastModified: new Date() }); + for (const key in updates) { + if (updates[key] !== undefined) { + task[key] = updates[key]; + } + } + + Object.assign(task, { lastModified: new Date() }); return task; }; diff --git a/examples/task-list/src/create-task.tsx b/examples/task-list/src/create-task.tsx index 977be37..bd80370 100644 --- a/examples/task-list/src/create-task.tsx +++ b/examples/task-list/src/create-task.tsx @@ -1,12 +1,12 @@ import { useState } from 'react'; -import { useTaskContext } from './task-context'; +import { useTaskActions } from './task-context'; type CreateTaskProps = { onSubmit: (title: string) => void; }; export const CreateTask = ({ onSubmit }: CreateTaskProps) => { - const { addTask } = useTaskContext(); + const { addTask } = useTaskActions(); const [title, setTitle] = useState(''); return ( diff --git a/examples/task-list/src/date-time.tsx b/examples/task-list/src/date-time.tsx index cf5bf36..ca37067 100644 --- a/examples/task-list/src/date-time.tsx +++ b/examples/task-list/src/date-time.tsx @@ -1,10 +1,10 @@ export const DateTime = ({ date, title }: { date: Date; title: string }) => { return ( -
+
{date.toLocaleString(undefined, {
dateStyle: 'short',
timeStyle: 'short',
diff --git a/examples/task-list/src/task-context.tsx b/examples/task-list/src/task-context.tsx
index 6222470..8993252 100644
--- a/examples/task-list/src/task-context.tsx
+++ b/examples/task-list/src/task-context.tsx
@@ -1,9 +1,11 @@
import {
createContext,
useReducer,
+ useMemo,
useEffect,
type ReactNode,
useContext,
+ useCallback,
} from 'react';
import { TasksActions, taskReducer, initialState } from './task-reducer';
import type { Task } from '../types';
@@ -27,7 +29,7 @@ const TaskProvider = ({ children }: TaskProviderProps) => {
const [state, dispatch] = useReducer(taskReducer, initialState);
// Fetch all tasks
- const fetchTasks = async () => {
+ const fetchTasks = useCallback(async () => {
dispatch({ type: TasksActions.SET_LOADING });
try {
const response = await fetch('/api/tasks');
@@ -39,66 +41,78 @@ const TaskProvider = ({ children }: TaskProviderProps) => {
payload: 'Failed to fetch tasks',
});
}
- };
+ }, [dispatch]);
// Add a new task
- const addTask = async (title: string) => {
- dispatch({ type: TasksActions.SET_LOADING });
- try {
- const response = await fetch('/api/tasks', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ title }),
- });
- const data = await response.json();
- dispatch({ type: TasksActions.ADD_TASK, payload: data });
- } catch (error) {
- dispatch({ type: TasksActions.SET_ERROR, payload: 'Failed to add task' });
- }
- };
+ const addTask = useCallback(
+ async (title: string) => {
+ dispatch({ type: TasksActions.SET_LOADING });
+ try {
+ const response = await fetch('/api/tasks', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ title }),
+ });
+ const data = await response.json();
+ dispatch({ type: TasksActions.ADD_TASK, payload: data });
+ } catch (error) {
+ dispatch({
+ type: TasksActions.SET_ERROR,
+ payload: 'Failed to add task',
+ });
+ }
+ },
+ [dispatch],
+ );
// Update a task
- const updateTask = async (id: string, updatedTask: Partial