Skip to content

Conversation

vtgrachev
Copy link
Contributor

@vtgrachev vtgrachev commented Apr 17, 2025

Hello!
"if (!error)" make cast value to boolean type and if value is cast to false then the error is skipped.

Sandbox: https://codesandbox.io/p/sandbox/6s9dw7

Example:

import useSWRMutation from "swr/mutation";

const fetcher = () => {
  return new Promise((_, reject) => reject("")); // or null/undefined/false/0
};

const Component = () => {
  const { trigger } = useSWRMutation("/example", fetcher);

  const handleClick = () => {
    trigger()
      .then(() => {
        console.log("skip error");
      })
      .catch(() => {
        console.log("error");
      });
  };

  return <button onClick={handleClick}>Execute</button>;
};
```

Copy link

codesandbox-ci bot commented Apr 17, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, can you add a test for it?

@vtgrachev vtgrachev force-pushed the fix-skip-error-if-value-cast-to-false branch from 3b38a3a to 853fa65 Compare April 17, 2025 08:59
@vtgrachev
Copy link
Contributor Author

Test ready

@vtgrachev vtgrachev requested a review from huozhi April 17, 2025 09:04
Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! looks good

@huozhi huozhi merged commit 87cf235 into vercel:main Apr 17, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants