3

I have a react app.

my routing looks like this =>

<AppRoute path="/profile" exact={true} menuTitle="Profile" component={pages.Profile} />
<AppRoute path="/profile/post_feed" exact={true} menuTitle="Post Feed" component={pages.PostFeed} />
<AppRoute path="/profile/story_feed" exact={true} menuTitle="Story Feed" component={pages.StoryFeed} />

now in profile, there is some filter, so I would like to save them in the URL, to do so, I am using the following.

  const setQueryParams = (filterId: number | null) => {
    history.push(`${urlParams.pathname}?filterId=${filterId}`);
  };



  const handleFilterSelectionChange = (id: number) => {
    setQueryParams(id);
    //....
  };

but this trigger a COMPLETE refresh of the page... and all the query that are associated. I put the following effect in my Profile component ( the one called from the route)

  useEffect(() => {
    console.log('FY');
  }, []);

And this trigger at load, then for each filter, the page is fully refreshed. But I would like those thing to trigger once, and when the route don't change (only the query parameters) those effect don7t get retriggered. Is it possible ?

1
  • 1
    You could either use shouldComponentUpdate or push to the window history directly. I have an application similar to google maps where I update the URL frequently, and I use the window history directly for updates that I know I do not want re-renders. Commented Dec 1, 2020 at 2:19

1 Answer 1

3

Try to use replace instead push

    const setQueryParams = (filterId: number | null) => {

        history.replace({ search: `?filterId=${filterId}` })// CHANGE HERE
    };



  const handleFilterSelectionChange = (id: number) => {
      setQueryParams(id);
    //....
  };
Sign up to request clarification or add additional context in comments.

6 Comments

it does the same that push T.T
Sorry I forgot a relevant detail, check it now
it does still reload the page. there must be something in my code then. this solution works to you ?
it's really annoying to debug ahah, just removing history.replace({ search: urlParams.search }); do the trick, but idk why it reload my route if I put it back.
If possible, provide more detail, like where ”history” came from
|

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.