// api.ts
const [todos, { mutate, refetch }] = createResource(fetchTodos)
async function fetchTodos(): Promise<Todo[]> {
const api = useApi()
return await api<Todo[]>('/user/todos')
}
export async function createTodo(
todo: { title: string; description: string; dueTime: Date },
): Promise<Todo> {
// ... (constructing request)
const createdTodo = await api<Todo>(
'/todos',
{ method: 'POST', body: requestBody },
)
mutate(todos => [...todos ?? [], createdTodo])
refetch() // <- this call causes the re-render
return createdTodo
}
export { todos as userTodos }
// api.ts
const [todos, { mutate, refetch }] = createResource(fetchTodos)
async function fetchTodos(): Promise<Todo[]> {
const api = useApi()
return await api<Todo[]>('/user/todos')
}
export async function createTodo(
todo: { title: string; description: string; dueTime: Date },
): Promise<Todo> {
// ... (constructing request)
const createdTodo = await api<Todo>(
'/todos',
{ method: 'POST', body: requestBody },
)
mutate(todos => [...todos ?? [], createdTodo])
refetch() // <- this call causes the re-render
return createdTodo
}
export { todos as userTodos }