-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Improve usability of the initiate()
action for mutations.
#4337
Improve usability of the initiate()
action for mutations.
#4337
Conversation
… improve usability
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
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. Latest deployment of this branch, based on commit 6aea6c8:
|
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Is there any reason why you wouldn't just: try {
const result = dispatch(api.endpoints.getPosts.initiate()).unwrap();
// do something with the result?
} catch (e) {
// handle error
} |
I guess I could, I didn't think of it :) Mainly following examples from: https://redux-toolkit.js.org/rtk-query/usage/usage-without-react-hooks |
Feel free to reject this change, the Thanks for your help! |
When running requests by manually dispatching the
initiate()
action the usability of mutation endpoints is worse than queries.When using a query you can easily check for the presence of a
data
orerror
prop in the union returned to make decisions. For example:In the case of mutation that code will have a type error:
Infact the only typesafe way to read the data or error property is to do something like this:
The reason it works for queries is because each entry in the discriminated union always includes an explicit
error
anddata
property, set to undefined if it's not included in that case.This PR makes the same change to the return type for mutations.