Repository navigation
Replies: 6 comments 15 replies
|
Would something like this proposal solve this issue without leaking the promise? I think it could be applied to |
|
This got discussed in Roadmap #5, around 31'50 The current idiomatic way to do this today is: let actionData = useActionData();
useEffect(() => {
if (actionData.ok) {
complete(actionData)
}
}, [actionData]);We may want to revisit returning promises from |
|
Someone also mentioned wanting a promise returned from |
|
Here's a simple function useSubmitPromise() {
const submit = useSubmit();
const navigation = useNavigation();
const actionData = useActionData();
const $deferred = useMemo(() => deferred(), []);
useEffect(() => {
if (navigation.state === "idle" && actionData) {
$deferred.resolve(actionData);
}
}, [$deferred, navigation.state, actionData]);
const _submit = useCallback(
(target: SubmitTarget, options: SubmitOptions = {}) => {
submit(target, options);
return $deferred.promise;
},
[$deferred.promise, submit]
);
return _submit;
}
// create a *deferred* promise
function deferred() {
let resolve: any;
let reject: any;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { resolve, reject, promise };
}And sample usage: const submit = useSubmitPromise();
const doSubmit = useCallback(
(e: any) => {
console.log("doSubmit");
e.preventDefault();
submit({ test: "123" }, { method: "post" }).then(
(data) => {
console.log(data);
}
);
},
[submit]
); |
|
same for |
|
This discussion is being closed as part of the Remix v3 repository cleanup. Remix v1/v2 was upstreamed into React Router v7, so if this proposal is still relevant, please reopen or continue the discussion in the React Router repository. |
Uh oh!
There was an error while loading. Please reload this page.
I would like to be able to submit an action from inside an event handler and call a function after the submit has finished. At the moment, my implementation looks something like this
completeFncould alternatively be stored in a ref instead, but the rest of the code remains largely the same.If
submitreturned a promise that resolved once the request had completed, it could look something like this insteadI have considered some alternatives, such as using
fetchto call the action directly, but decided against it as it would essentially mean opting out of Remix's transition management, which is one of the big reasons I'm using Remix in the first place.A similar discussion already exists here #3955 but it wasn't actually raised as a proposal which is why I decided to start this one separately.
All reactions