-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hooks: useUpdate onError signature typescript issue #7653
Comments
Hi! In the meantime, here is some code inspired by the way we handle the default onError behaviour in the useEditController that you cas use as a workaround: const notify = useNotify();
const [update] = useUpdate(
'users',
{
id: 42,
data: { email: 'foo@bar.com' },
},
{
onError: (error: Error | string) => {
notify(`Error while updating: ${typeof error === 'string'
? error
: error.message}`);
}
}
) |
react-query's const notify = useNotify();
const [update] = useUpdate<User, Error | string>(
'users',
{
id: 42,
data: { email: 'foo@bar.com' },
},
{
onError: (error) => { // <= react-query will type the error correctly
notify(`Error while updating: ${typeof error === 'string'
? error
: error.message}`);
}
}
) Until we implement that, you can narrow the type, as explained in React Query and TypeScript: const notify = useNotify();
const [update] = useUpdate(
'users',
{
id: 42,
data: { email: 'foo@bar.com' },
},
{
onError: (error) => {
const message = error instanceOf Error ? error.message : error instanceOf String ? error : undefined;
notify(`Error while updating: ${message}`);
}
}
) |
@fzaninotto I copy/paste your sample for trying, however it trigger me a lot of syntax error: src/resources/credit-cards/components/CreditCardValidField.tsx:28:64 - error TS1005: ',' expected.
28 const message = error instanceOf Error ? error.message : error instanceOf String ? error : undefined;
~
src/resources/credit-cards/components/CreditCardValidField.tsx:28:72 - error TS1005: ',' expected.
28 const message = error instanceOf Error ? error.message : error instanceOf String ? error : undefined;
~~~~~~~~~~
src/resources/credit-cards/components/CreditCardValidField.tsx:28:83 - error TS1005: ',' expected.
28 const message = error instanceOf Error ? error.message : error instanceOf String ? error : undefined;
~~~~~~
src/resources/credit-cards/components/CreditCardValidField.tsx:28:92 - error TS1005: ',' expected.
28 const message = error instanceOf Error ? error.message : error instanceOf String ? error : undefined;
~~~~~
src/resources/credit-cards/components/CreditCardValidField.tsx:28:109 - error TS1005: ',' expected.
28 const message = error instanceOf Error ? error.message : error instanceOf String ? error : undefined;
~
src/resources/credit-cards/components/CreditCardValidField.tsx:30:16 - error TS1003: Identifier expected.
30 notify(`Error while updating: ${message}`);
~~~~~~~~~~~~~~~~~~~~~~~~~ Am I missing something here? 🤔 |
For now, my workaround was to silent the error until resolution: // @ts-expect-error https://github.com/marmelab/react-admin/issues/7653
onError: (error: Error) => {
notify(`Echec de la mise à jour : ${error.message}`, {
type: 'error',
});
}, |
@soullivaneuh just to be sure, did you try with |
@slax57 Well... 🙃 Next time I'll re-read my copy/paste, thanks! 😉 My setup: const [setup, { isLoading }] = useUpdate(
'users',
{
},
{
onSuccess: () => {
refresh();
},
onError: (error) => {
const message = error instanceof Error ? error.message : error instanceof String ? error : undefined;
notify(`Echec : ${message}`, {
type: 'error',
});
refresh();
},
},
); Works! However, this double ternary shows indeed the need of a stronger typing to be provided by |
Since the v4 upgrade of react-admin, I have the following typing issue on the
useUpdate
function when I try to access theerror.message
property of the parameter provided by theonError
function:I also try to specify the type of the error like that:
(error: Error) => ...
, but this leads to the following another error:Steps to reproduce:
npm start
Related code:
Sandbox PR: soullivaneuh/react-admin-sandbox#1
Other information:
Environment
The text was updated successfully, but these errors were encountered: