Replies: 10 comments 14 replies
-
I agree. |
Beta Was this translation helpful? Give feedback.
-
I'd also like a solution to this. As far as I can tell there isn't even a way to know if an action error'd except sending down an error message from the action. But even then, because there's no way to reset a fetcher, the error data will always exist. Is there any good solutions around this? |
Beta Was this translation helpful? Give feedback.
-
There is nothing preventing you from using native |
Beta Was this translation helpful? Give feedback.
-
Definitely need this feature @ryanflorence. Without promise support it's very annoying and buggy to implement infinite scrolling. The reason being you have to rely on
If some other state changes then the above effect will run again because With promise supported, we can simply do |
Beta Was this translation helpful? Give feedback.
-
@iway1 @krall12 @juniorforlife - I created 2 lightweight hooks over useSubmit and useFetcher that look like this: const {submit, ...} = useRemixFetcher({
onSuccess(){ /*show toast or notification*/},
onError(){..}
})
const {submit, ...} = useRemixSubmit({
onSuccess(){ /*show toast or notification*/},
onError(){..}
}) You can subscribe to this Remix Discussion proposal to get updates or give feedback in the link below: |
Beta Was this translation helpful? Give feedback.
-
Hello Remixers, I'll leave my strategy here, in case anyone runs into this "problem". Following the tip that @kiliman left us here https://gist.github.com/kiliman/c900df084ea7dd306e0cb85b81848fbb (thanks @kiliman for this snippet 💯 ) this an hook: import { Fetcher } from "@remix-run/react";
import { useEffect, useRef } from "react";
export const useToastSubmitPromise = (initialState: Fetcher<any>) => {
const promisefy = useRef(deferred());
useEffect(() => {
if (initialState.state === "idle" && initialState.data && initialState.data.success) {
promisefy.current.resolve();
promisefy.current = deferred(); // reset promise state for pending.
}
if (initialState.data && !initialState.data.success) promisefy.current.reject();
}, [initialState.state, initialState.data]);
return promisefy.current.promise
}
function deferred() {
let resolve: any;
let reject: any;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { resolve, reject, promise };
} where its use can be like this: const submit = useToastSubmitPromise(fetcher);
useEffect(() => {
if (fetcher.state === "idle") return;
toast.promise(submit,
{
pending: 'Salvado dados, por favor aguarde...',
success: 'Dados salvos com sucesso.',
error: 'Ops, ocorreu um erro, tente novamente.',
},
{ toastId: 'submit_data' }
);
}, [fetcher.state]) |
Beta Was this translation helpful? Give feedback.
-
Haven't tested yet, but I guess now we can achieve that with client actions. For example: export async function clientAction({
serverAction,
}: ClientActionFunctionArgs) => {
const data = await serverAction()
toast.show('Success!')
return data
} |
Beta Was this translation helpful? Give feedback.
-
This is my biggest gripe with Remix after using it for a little over a year. Everything having to do with reacting to form submission results, whether it's "close this form after the form submits" or "show a toast" requires me to think hard about this problem that is fairly well-understood in other systems. Maybe what's provided is just enough to implement custom hooks on top, but to me it feels too vague and isn't very well documented either - the docs don't give many hints, there's no guide for this case, and as this thread and others suggest, a lot of people aren't getting it. |
Beta Was this translation helpful? Give feedback.
-
`
` This is a variant I use |
Beta Was this translation helpful? Give feedback.
-
Looks like we will finally see this long awaited feature: https://github.com/orgs/remix-run/projects/5?pane=issue&itemId=62177552 🙏 |
Beta Was this translation helpful? Give feedback.
-
Remix doesn't have any straightforward way to run some code after a successful request is sent with
useSubmit
oruseFetcher
. It feels extremely clunky in certain situations. For example, if I want to show a toast to the user imperatively (which is the easiest way to show toasts), then I have to do something like this:Am I the only one that think this just really, really sucks?
it's really easy to mess up and show the toast multiple times, and it's way clunkier than having
.submit()
be a promise that we can just.then
. This would be so much easier and less error prone:Pretty much every other javascript interface in existence that allows sending data is promise based. I understand remix wants to do things differently and encourage good practices, but
.then
seems to be much better for these types of situations.I really can't think of a good reason to make it this difficult to just run some code after a successful request.
Beta Was this translation helpful? Give feedback.
All reactions