`react
小智 6
接受的答案不正确。onSuess/onError 处理程序也可用于 'mutate' 方法(react-query.tanstack./reference/useMutation)。
这里的问题是,如果您的组件在突变完成之前卸载,这些额外的回调将不会运行。因此,您必须确保执行突变的组件不会卸载。在你的情况下:
const { mutate } = useAddFAQPost() const onSubmit = useCallback((event: React.ChangeEvent<FormEvent>) => { event.preventDefault(); return mutate({ title, type } as FAQ), { onSuess: async (data: string, context: string) => { console.log(data); console.log('why not?'); }, onError: async (data: string, context: string) => { console.log(data); }, onSettled: () => { //Some unmounting action. //eg: if you have a form in a popup or modal -> call your close modal method here. // onSettled will trigger once the mutation is done either its sueeds or errors out. } }; }, [title, type])react