1

I'm trying to reload my page but keep the filters state with Inertia.

Like this:

const [allProducts, setAllProducts] = useState([]);
    
const [filters, setFilters] = useState([]);

const fetchProducts = (page) => {
    Inertia.get(
      `/products?page=${page}`,
      { filters },
      {
        preserveState: true,
        preserveScroll: true,
        only: ['products'],
        onSuccess: (response) => {
          setAllProducts(allProducts.concat(response.props.products.data));
          page += 1;
        }
      }
    )
});

The problem is that filters and allProducts are reset even though preserveState = true.

2 Answers 2

0

What do you mean by "reload my page"? State in React is not preserved on "real" page refresh.

Maybe you can save data in local storage, then load it by useEffect. Another solution is to store data you want to keep between pages in Laravel session storage.

Sign up to request clarification or add additional context in comments.

Comments

0

In your Controller try to use withQueryString() method. as a Example:

$allUsers = User::where('id', '!=', Auth::id())
            ->when($request->input('search'), function ($query, $search) {
                $query->where(function ($query) use ($search) {
                    $query->where('name', 'like', '%' . $search . '%')
                        ->orWhere('email', 'like', '%' . $search . '%');
                });
            })
            ->paginate(10)
            ->withQueryString()
            ->onEachSide(2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.