Skip to content

Conversation

@gronxb
Copy link

@gronxb gronxb commented Jan 11, 2024

Summary

Inspired by React Router's useSearchParams,

The useNavigate method is useful for navigating between pages. However, when performing filter-related functions on the current page, it's often necessary to set the state within the page itself. In such cases, a combined setter for search parameters and state would be more beneficial.

2024-01-12.12.09.42.mov

image

Simple Usage

const countRoute = new Route({
  getParentRoute: () => rootRoute,

  validateSearch: (search) =>
    search as {
      count: number;
      id: string;
    },
  path: "count",
  component: PostComponent,
});

export function PostComponent() {
  const { count } = countRoute.useSearch();
  const setSearch = countRoute.useSetSearch();
  // replace false
  // const setSearch = countRoute.useSetSearch({ replace: false }});

  return (
    <div>
      Count {count}
      <button
        onClick={() => {
          setSearch({
            count: count + 1,
          });
        }}
      >
        setSearch
      </button>{" "}
    </div>
  );
}

@schiller-manuel
Copy link
Contributor

why wouldn't you use useNavigate to update the search params without navigating away?

@gronxb
Copy link
Author

gronxb commented Jan 12, 2024

@schiller-manuel

I think useNavigate does more than just replace search.

It's possible with useNavigate, but useNavigate has a good scenario between screen moves, and I think what I've done is separation of concerns.

A scenario where you only want to control the search state on the current page, without page navigation.

For a more detailed scenario, the list is rendered on the current page and you can select a sort by filter. In this case, you'll only want to change and integrate the search state rather than navigate.

@schiller-manuel
Copy link
Contributor

I use useNavigate to update the state of a filtered, paginated list like this:

const navigate = useNavigate({from: '/foo'});
navigate({search: (prev) => ({...prev, page: page + 1 });

@gronxb gronxb closed this Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants