Inconsistencies of `useCachedPromise` when using `Action.Push`
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 7.8k
- Forks
- 6.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 442
Description
Description
I am experiencing a strange behavior where the useCachedPromise is inconsistent when using Action.Push. I say it is inconsistent because it does not always behave the same way. I was unable to pinpoint exactly when or how it occurs.
Here are a couple of videos showing the issue:
https://github.com/raycast/extensions/assets/36263538/22250b2b-1d54-4294-b52d-b70c934b2d50
https://github.com/raycast/extensions/assets/36263538/f7d76736-e90f-4d31-9002-aa68d8dc9b21
Steps To Reproduce
Create a new command with the following code:
import { Action, ActionPanel, Form, List, showToast, Toast } from "@raycast/api";
import { useCachedPromise } from "@raycast/utils";
const database = ["Walk the dog"];
const getTodos = async () => [...database];
const addTodo = async (title: string) => database.push(title);
function useTodos() {
const { data: todos, mutate } = useCachedPromise(async (key) => getTodos(), ["todos"]);
return {
todos,
addTodo: async (title: string) => {
const toast = await showToast({ style: Toast.Style.Animated, title: "Creating todo" });
await mutate(addTodo(title));
toast.style = Toast.Style.Success;
toast.title = "Todo created";
},
};
}
function CreateForm() {
const { addTodo } = useTodos();
return (
<Form
actions={
<ActionPanel>
<Action.SubmitForm title="Create Todo" onSubmit={(values) => addTodo(values.title)} />
</ActionPanel>
}
>
<Form.TextField title="Title" id="title" />
</Form>
);
}
export default function ListView() {
const { todos } = useTodos();
return (
<List>
{todos?.map((todo) => (
<List.Item
key={todo}
title={todo}
actions={
<ActionPanel>
<Action.Push title="Create" target={<CreateForm />} />
</ActionPanel>
}
/>
))}
</List>
);
}
Current Behaviour
Sometimes the previous List view doesn't get updated when calling mutate from the useCachedPromise hook.
Expected Behaviour
Calling mutate should properly revalidate the previous views in the stack.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the sample TypeScript command, focusing on the useCachedPromise hook, mutate call, and Action.Push navigation between ListView and CreateForm. Compare whether the previous List view revalidates after adding a todo; done means mutate consistently updates views already in the navigation stack.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100