Hi folks, looking for some help with using the new preload query features and react router.
I’m building an inventory management app, and after adding or deleting a product, returning to the All Products page doesn’t display the newly added/ deleted product.
i’m using the new createBrowserRouter (v 6.4 of React router) and the affected Route looks like so:
{
path: '/products',
element: <ProductPage />,
loader: allProductsLoader,
children: [
{
path: '/products/:productID',
element: <Product />,
loader: findProductLoader,
},
{
path: '/products/add-product',
element: <AddProduct />,
},
],
},
the allProduct loader looks like so:
export const allProductsLoader = async () => {
return preloadQuery(ALL_PRODUCTS, {
fetchPolicy: 'network-only',
errorPolicy: 'all',
})
}
addProduct action looks like so:
export const addComponent = async (name: string, cost: number, stock: number) => {
try {
const { data } = await client.mutate({
mutation: ADD_COMPONENT,
variables: { name, cost, stock },
})
return data.addComponent
} catch (error: unknown) {
return error as Error
}
}
and finally the product page looks like so:
const ProductPage: React.FC = () => {
const queryRef = useLoaderData()
const { data: loaderData, error } = useReadQuery(queryRef as QueryRef<loaderData>)
if (error) {
notify({ error: error.message })
return <div>Can't display page</div>
}
return (
<div className="container mx-auto px-4 py-8">
<ProductList products={loaderData.allProducts} />
</div>
)
}
(ProductList is just a TanStack Table displaying the product data)
Something worth mentioning though, ProductList displays the AddProduct page in a modal like so:
{location.pathname != '/products' && location.pathname != '/' && (
<Modal onClose={() => navigate('/products')}>
<Outlet />
</Modal>
)}
so I’m technically never leaving the /products url, when pressing addProduct i go to /products/:productId and after it is successfully added, I navigate back to /products.
I’ve tried a bunch of approaches like navigating to the same page again, useRevalidator, etc… to force a refetch but nothing short of refreshing the page or going to a different page and returning works.
Thanks in advance.