John V
John V

Reputation: 875

Not redirect to the same component in react

My component not redirect to same component with different parameters.

    <BrowserRouter basename={process.env.REACT_APP_DEFAULT_PATH ?? ''}>
        <Switch>
            ...
            <Route exact path={"/admin/logs/:page"} component={Logs} key={"Logs"} />
            ...
        </Switch>
    </BrowserRouter>

And in component:

    this.props.history.push("/admin/logs/" + page);

It change the url in browser, but not redirect

Upvotes: 3

Views: 200

Answers (1)

Drew Reese
Drew Reese

Reputation: 202608

Since your component is already matched and mounted/rendered it now needs to also "listen" for changes to the route props that are passed to it.

I see that you are using a class-based component from this.props.history.push("/admin/logs/" + page); so you need to implement the componentDidUpdate lifecycle method.

componentDidUpdate(prevProps) {
  if (this.props.match.params.page !== prevProps.match.params.page) {
    // page updated, do something
  }
}

Upvotes: 2

Related Questions