Reputation: 105071
I have a component (like a dialog box) that provides the Submit button. This dialog component then renders a form component.
Form component includes all the fields and also RTK Query to send the mutation to backend.
I would like to check the RTK Query endpoint state from the dialog component so that I can set the loading state of the submit button. Ideally hook to state changes or subscribe... If nothing else using a useEffect
to run when the state changes.
I've tried using useSelector(state => ...)
that can get me the RTK state including all endpoints, but it seems that mutations always just have the "fulfilled" state value.
How can I do this?
I'm currently passing a react state from the dialog to the form:
Dialog:
const loadingState = useState<boolean>(false);
// render
<Form state={loadingState} ... />
Form:
const [_, setLoading] = props.state;
const [update] = api.useUpdateMutation();
const onSubmit = (data) => Promise.resolve()
.then(() => setLoading(true))
.then(() => update(data).unwrap())
.finally(() => setLoading(false))
This works, but it would be better if the parent component that renders the dialog and the form within would just hook onto the RTK Query update mutation endpoint state changes.
Upvotes: 1
Views: 61
Reputation: 203333
Two solutions I see being viable are:
fixedCacheKey
Use a shared fixedCacheKey
for the update mutation and access the useUpdateMutation
hook's result data in both components. This allows all instances of the mutation hook to share results.
See Shared Mutation Results for details.
Example
Dialog:
const [, { isLoading }] = api.useUpdateMutation({
fixedCacheKey: "update"
});
// render
<Form ... />
<button
disabled={isLoading}
type="submit"
>
Submit
</button>
Form:
const [update] = api.useUpdateMutation({
fixedCacheKey: "update"
});
const onSubmit = async (data) => {
try {
...
await update(data).unwrap();
// mutation success
} catch(error) {
// Catch & handle/ignore any submission errors/rejections
}
};
Hoist the useUpdateMutation
to the parent Dialog
component and pass down the update
trigger function as props to the Form
.
Example
Dialog:
const [update, { isLoading }] = api.useUpdateMutation();
// render
<Form update={update} ... />
<button
disabled={isLoading}
type="submit"
>
Submit
</button>
Form:
const onSubmit = async (data) => {
try {
...
await props.update(data).unwrap();
// mutation success
} catch(error) {
// Catch & handle/ignore any submission errors/rejections
}
};
Upvotes: 0