我正在尝试使用 SWR 和 SSR 创建带有分页的产品列表。为了做到这一点,我将初始数据传递给 SWR,当分页更改时,我调用一个 mutate 函数,该函数绑定到第一个 SWR 请求。
问题是页面没有重新渲染,当我打印 mutate 函数的结果时,我看到数据已经更改,但页面没有重新渲染,我不明白为什么。
代码:
const ProductList = ({initialData, endpoint, productLimit, productCount}) => {
const [pageIndex, setPageIndex] = useState(1)
const [isLoading, setIsLoading] = useState(false);
console.log(`${endpoint}?_limit=${productLimit}&_start=${pageIndex * productLimit ?? 0}`)
const {data, mutate} = useSWR(
`${endpoint}?_limit=${productLimit}&_start=${pageIndex * productLimit ?? 0}`,
fetcher,
{initialData: initialData}
)
return (
<div className={cls([styles.product_list_container])}>
<div>
<div className={cls([styles.product_list])}>
{data.map(product => {
return (
<ProductCard productData={product}/>
)
})}
</div>
<Pagination
pageIndex={pageIndex}
onclick={async (index) => {
setPageIndex(index);
setIsLoading(true)
mutate().then(resp => …Run Code Online (Sandbox Code Playgroud) 我的 HttpOnly cookie 有问题,我有一个自定义身份验证 API,它会在我登录时返回带有令牌的 HttpOnly cookie,但问题是,当我使用 withCredentials 在 Axios 前面发出另一个请求时,该 cookie 不会发送标题,我不明白为什么。
我使用 NextJs 作为前端,使用 Strapi 作为后端
这是创建第二个请求的代码
await axios.get(param.endpoints.getMe, {withCredentials: true})
Run Code Online (Sandbox Code Playgroud)