reactjs - 当仅参数更改但更改浏览器中的 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 发生变化,但没有任何反应。
我实际上并没有重定向到其他个人资料。
注意: 对于许多开发人员来说,这似乎是一个反复出现的问题。建议的解决方案都不适合我。所以我写这篇文章是因为我怀疑我的情况有点不同。
解决方案
推荐阅读
- sql - 如何比较 2 个不同数据库中的 2 个表并仅更新更改的值?
- javascript - 创建一个 isAllX 以确定整个字符串是由小写 x 还是大写 X 组成。如果是则返回 true,否则返回 false。- Javascript
- php - 将 Cloudflare CDN 集成到 laravel
- javascript - 将 api 调用和 try catch 块抽象为 JavaScript 中的一种常用方法,使其更可重用
- kubernetes - 查找正在使用的 kubeconfig 文件
- elixir - 如何在 Elixir / Plug 中访问文件上传 / POST
- flutter - 如何将一个用户的通知发送给另一个用户 Signal & Flutter
- dart - 如何在 Dart 中为一些 JSON 数据创建容器?
- c++ - cocos2dx 中的操纵杆 - 版本 4
- php - 使用 AJAX 和 PHP 的实时进度状态报告