Rez*_*zar 10 mutation reactjs rtk-query
在 React RTK Query Mutation中可以获得端点的响应。我有一个插入数据库的突变,我需要获取插入的 ID。在我的 API 中:
addRecord: build.mutation({
query(data) {
return {
url: base + 'myControler/SaveDataAsync',
method: 'post',
data: data,
}
},
}),
Run Code Online (Sandbox Code Playgroud)
在导入我的钩子后在我的组件中我称它为
const [addRecord] = useAddRecordMutation();
Run Code Online (Sandbox Code Playgroud)
然后在我的提交函数中使用它
const handleSubmitCustom = async (values) => {
await addRecord(values);
}
Run Code Online (Sandbox Code Playgroud)
我需要await addRecord(values);的返回值
phr*_*hry 14
你可以这样做
const handleSubmitCustom = async (values) => {
try {
const returned = await addRecord(values).unwrap();
} catch (error) {
// you can handle errors here if you want to
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7130 次 |
| 最近记录: |