gpt4 book ai didi

reactjs - NavLink 或 Link inside the nav of react-router-DOM is updating the URL .But it is not updating the component

转载 作者:行者123 更新时间:2023-12-05 07:15:52 24 4
gpt4 key购买 nike

react-route-dom 的 Nav 链接改变了 Url 的路由但不渲染组件。但是,如果我们重新加载该路由,它就会起作用。

导航栏代码

import React from "react";
import { connect } from "react-redux";
import compose from "compose";
import { Link, NavLink, BrowserRouter, withRouter } from "react-router-dom";

const NavBar = ({ currentUser: user }) => {

if (user) {
for (var a in user) {
if (a === "user") {
user = user[a];
}
}
}

return (
<nav className="navbar navbar-expand-lg navbar-dark bg-primary">
<BrowserRouter>
<Link className="navbar-brand" to="/">
Users UI
</Link>
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup"
aria-expanded="false"
aria-label="Toggle navigation"
>

<span className="navbar-toggler-icon" />
</button>
<div className="collapse navbar-collapse" id="navbarNavAltMarkup">
<div className="navbar-nav" style={{ cursor: "pointer" }}>
{user && (
<React.Fragment>
<a className="nav-item nav-link" href={`/user/${user._id}`}>
{"Edit Profile Mr." + user.name}
</a>
<a className="nav-item nav-link" href="/logout">
Logout
</a>
</React.Fragment>
)}
</div>
</div>
</BrowserRouter>
</nav>
);
};

const mapStateToProps = state => ({
currentUser: state.user.currentUser
});

app.js 代码

import React, { Component } from "react";
import { Switch, Route, Redirect, } from "react-router-dom";
import { connect } from "react-redux";
import auth from "./services/authService";
import ProtectedRoute from "./components/common/protectedRoute";
import NavBar from "./components/navBar";
import NotFound from "./components/notFound";
import LoginForm from "./components/login";
import Logout from "./components/logout";
import AddUser from "./components/addUser";
import User from "./components/user";
import { setCurrentUser } from "./Redux/user/user-action";
import "./App.css";

class App extends Component {
state = {};
componentDidMount() {
const { setCurrentUser } = this.props;
const user = auth.getCurrentUser();

setCurrentUser({ user });
}
render() {
return (
<React.Fragment>
<NavBar />
<main className="container">
<Switch>
<Route exact path="/login" component={LoginForm} />
<ProtectedRoute path="/user/:id" component={AddUser} />
<ProtectedRoute exact path="/" component={User} />
<ProtectedRoute exact path="/logout" component={Logout} />
<Route path="/not-found" component={NotFound} />
<Redirect to="/not-found" />
</Switch>
</main>
</React.Fragment>
);
}
}

redux 代码

const mapDispatchToProps = dispatch => ({
setCurrentUser: user => dispatch(setCurrentUser(user))
});
export default connect(null, mapDispatchToProps)(App);

index.js代码

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { BrowserRouter as Router } from "react-router-dom";
import { Provider } from "react-redux";
import store from "./Redux/store";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(
<Provider store={store}>
<Router>
<App />
</Router>
</Provider>,
document.getElementById("root")
);
/*
If you want your app to work offline and load faster, you can change
unregister() to register() below. Note this comes with some pitfalls. */

serviceWorker.unregister();

最佳答案

删除 BrowserRouter来自 NavBar成分。

Linkswitch必须包裹在相同的 Router 中,您已经在 <App/> 附近提供了. Link里面BrowserRouter (来自 NavBar )没有对应的 switch ,这就是页面没有改变的原因。

关于reactjs - NavLink 或 Link inside the nav of react-router-DOM is updating the URL .But it is not updating the component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com