reactjs - 反应路由器链接不刷新页面
问题描述
早上好,我有一个带有导航栏的标题组件和其中一些来自反应路由器的链接,标题在我的应用程序组件中被调用,并且链接具有一些与身份验证相关的显示条件。问题是,当我单击标题上的任何链接时,页面会更改,但标题本身保持不变,例如登录,当我单击登录时注销登录组件应该被隐藏并显示注销组件,但它不起作用。Ps:当我改变一切都很好
function TodoApp() {
// let isUserLoggedIn = AuthenticationService.isUserLoggedIn();
// console.log(isUserLoggedIn);
// render() {
return (
<div className="TodoApp">
{ <Router>
<div>
<HeaderComponent isUserLoggedIn={AuthenticationService.isUserLoggedIn} />
<Switch>
<Route path="/" exact component={withRouter(LoginComponent)} />
<Route path="/login" component={withRouter(LoginComponent)} />
<AuthenticatedRoute
path="/welcome/:name"
component={withRouter(WelcomeComponent)}
/>
<AuthenticatedRoute
path="/todos"
component={withRouter(ListTodosComponent)}
/>
<AuthenticatedRoute
path="/logout"
component={withRouter(LogoutComponent)}
/>
<Route component={withRouter(ErrorComponent)} />
</Switch>
<FooterComponent />
</div>
</Router>
{/* <LoginComponent /> */}
</div>
);
}
}
class HeaderComponent extends Component {
// constructor(props) {
// super(props);
// this.state = {
// isUserLoggedIn: AuthenticationSevice.isUserLoggedIn(),
// };
// // this.handleState = this.handleState.bind(this);
// }
componentDidMount() {
console.log("Header charged");
}
// handleState = () => {
// this.setState(() => {
// return { isUserLoggedIn: AuthenticationSevice.isUserLoggedIn() };
// });
// console.log(this.state.isUserLoggedIn);
// }
render() {
// const isUserLoggedIn = AuthenticationSevice.isUserLoggedIn();
return (
<header>
<nav className="navbar navbar-expand-md navbar-dark bg-dark">
<div>
<a className="navbar-brand" href="https://github.com/sbouhaddi">
Sbouhaddi
</a>
</div>
<ul className="navbar-nav">
{this.props.isUserLoggedIn && (
<li>
<Link className="nav-link" to="/welcome/Sbouhaddi">
Home
</Link>
</li>
)}
{this.props.isUserLoggedIn && (
<li>
<Link className="nav-link" to="/todos">
Todos
</Link>
</li>
)}
</ul>
<ul className="navbar-nav navbar-collapse justify-content-end">
{!this.props.isUserLoggedIn && (
<li>
<Link className="nav-link" to="/login">
Login
</Link>
</li>
)}
{this.props.isUserLoggedIn && (
<li>
<Link
className="nav-link"
onClick={AuthenticationSevice.logout}
to="/logout"
>
Logout
</Link>
</li>
)}
</ul>
</nav>
</header>
);
}
}
解决方案
您似乎没有调用AuthenticationService
登录函数来将值传递给isUserLoggedIn
道具。
AuthenticationService.isUserLoggedIn
对比AuthenticationService.isUserLoggedIn()
<HeaderComponent isUserLoggedIn={AuthenticationService.isUserLoggedIn()} />
推荐阅读
- javascript - jQuery 中的 focus() 和 focusout()
- javascript - 多页中包含单独的html文件时,在页眉中显示页标题
- html - 内联 SVG
<desc> correct usage for accessilibility</h1> <div id="body"><p>I am using a lot of inline svgs in my html and am a little confused about the best way to present them concerning accessibility.</p> <p>I've - phpunit - 执行测试时多次调用phpunit设置函数
- mongodb - 出现错误“连接测试失败:querySrv ENOTFOUND _mongodb._tcp.127.0.0.1”
- spring - Spring + OpenFeign:POST 导致 NoSuchBeanDefinitionException
- python - 在 apache flink 中嵌入现有的 ML 模型
- sql - 基于列的 SQL 中的 SUM 值
- .net - 通过 Azure AD 进行 .NET Core WebApp 身份验证,但从数据库中提取数据
- angular - 角度路由给出错误类型错误:无法读取未定义的属性“init”