首页 > 解决方案 > 当仅参数更改但更改浏览器中的 url 时,History.push 不会重定向

问题描述

我有这条路线:

   <PrivateRoute path="/profile/:user_id" exact component={GenericProfile} />;

这是PrivateRoute组件:

const PrivateRoute = ({ component: Component, auth, ...rest }) => {
  return (
    <Route
      {...rest}
      render={(props) =>
        auth.isAuthenticated === true ? (
          <Component {...props} />
        ) : (
          <Redirect to="/login" />
        )
      }
    />
  );
};

PrivateRoute.propTypes = {
  auth: PropTypes.object.isRequired,
};

const mapStateToProps = (state) => ({
  auth: state.auth,
});

export default connect(mapStateToProps)(PrivateRoute);

这是它在App.js中的使用方式:

当我在个人资料页面中并且我有一个重定向到另一个个人资料的按钮时:

    <Button onClick={()=>{
       this.props.history.push("/profile/" + another_user_id)
                   }}>
      Redirect to another profile
    </Button>

浏览器中的 url 发生变化,但没有任何反应。
我实际上并没有重定向到其他个人资料。

注意: 对于许多开发人员来说,这似乎是一个反复出现的问题。建议的解决方案都不适合我。所以我写这篇文章是因为我怀疑我的情况有点不同。

标签: reactjsreact-routerreact-router-domreact-router-v4react-router-redux

解决方案


推荐阅读