【发布时间】:2022-02-23 17:36:42
【问题描述】:
我正在使用带有 SWR 的 NextJS,并且我想使用 SWR 获取一个特定 ID 的数据。 当用户点击表格行时,它将从服务器中提取具有该 ID 的数据。
下面是 SWR 代码
export const apiExpenses = () => {
const { data: showExpense, error, mutate } = useSWR('/api/expenses/show', () =>
axios
.get('/api/expenses/show/' + expenseId)
.then(res => res.data)
)
return {
showExpense,
}
};
下面的代码是另一个调用showExpense的组件,
const { showExpense } = apiExpenses();
const clickTo = async (expenseId) => {
//How can i fetch data with specific ID in SWR?
const expenseData = //Code?
};
//In JSX
<IconButton onClick={() => clickTo(params.id)} color="primary">
我不确定如何将expenseId 传递给 SWR API。
谢谢,
【问题讨论】: