gpt4 book ai didi

javascript - 在React中使用react-router时组件消失

转载 作者:行者123 更新时间:2023-12-01 00:06:40 26 4
gpt4 key购买 nike

所以,我一直在构建一个React应用程序,并且我一直面临刷新时组件消失的类似问题,更确切地说,它与React路由器有关,我一定是错误地使用了它

App.js 看起来像

import React from "react";
import "./App.css";
import { Home } from "./components/home";

function App() {
return <Home />;
}

export default App

主页组件

export class Home extends Component {
render() {
return (
<BrowserRouter>
<NavigationBar />
<Switch>
<Route path="/jobPortal" component={JobPortal} />
</Switch>
</BrowserRouter>
);
}

通用导航栏

const NavigationBar = ({ userType }) => {
return (
<Navbar style={globalStyles.container} bg="dark" variant="dark">
<Navbar.Brand style={globalStyles.container} href="/">
Soffice
</Navbar.Brand>
<Nav className="ml-auto">
<Nav.Link as={NavLink} to="/JobPortal">
Job Portal
</Nav.Link>
{userType && (
<Nav.Link as={NavLink} to="/logout">
Log Out
</Nav.Link>
)}

{!userType && (
<React.Fragment>
<Nav.Link as={NavLink} to="/SignUp">
Sign Up
</Nav.Link>
<Nav.Link as={NavLink} to="/SignIn">
Sign In
</Nav.Link>
</React.Fragment>
)}
</Nav>
</Navbar>
);
};

导出默认导航栏;

访问 jobPortal 时出现的职位导航栏

const NavigationBar = ({ url }) => {
// let {url,url} =useRouteMatch();
console.log(url);
return (
<Navbar style={{ ...globalStyles.container }} bg="info" variant="dark">
<Nav className="mx-auto">
<Nav.Link style={styles.navBarColor} as={NavLink} to="/JobPortal">
Home
</Nav.Link>
<Nav.Link style={styles.navBarColor} as={NavLink} to={`${url}/Post`}>
Post Job
</Nav.Link>
<Nav.Link style={styles.navBarColor} as={NavLink} to={`${url}/Want`}>
Want Job
</Nav.Link>
<Nav.Link style={styles.navBarColor} as={NavLink} to={`${url}/Contact`}>
Contact
</Nav.Link>
</Nav>
</Navbar>
);
};
const styles = {
navBarColor: {
color: "white",
fontSize: 22
}
};
export default NavigationBar;

JobPortal 组件

export class JobPortal extends Component {
componentDidMount() {
console.log("path", this.props.match.url);
}
render() {
const path = this.props.match.path;

return (
<BrowserRouter>
<NavigationBar url={this.props.match.url} />
<Switch>
<Route path={`${path}/Want`} component={Want} />
</Switch>
</BrowserRouter>
);
}
}

想要组件

class Want extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
email: "",
message: "",
jobs: [
{
jobId: 1,
jobTitle: "software"
}
]
};
}

handleClick() {
console.log(this.state);
}
componentWillMount() {
this.url = this.props.location.pathname;
console.log("location", this.props.location.pathname);
}
render() {
const styles = {
card: {
width: "14em",
height: "100%",
fontSize: 20,
padding: 10
}
};
return (
<BrowserRouter>
<Route path={`${this.url}/:jobId`} component={Job} />
<Container>
<Container></Container>
<Container>
<Row>
{this.state.jobs.map((job, index) => (
<Col style={{ padding: 20 }}>
<Card text="white" bg="dark" style={styles.card}>
<Card.Body>
<Card.Title>
<Link to={`${this.url}/${job.jobId}`}>
{job.jobTitle}
</Link>
</Card.Title>
</Card.Body>
</Card>
</Col>
))}
</Row>
</Container>
</Container>
</BrowserRouter>
);
}
}

export default Want;

正在消失的工作组件

export class Job extends Component {
render() {
return <p>this will disappear</p>;
}
}

完整代码沙箱链接

https://codesandbox.io/s/mystifying-yalow-6jcyn?fontsize=14&hidenavigation=1&theme=dark

当我点击软件链接时,卡片应该消失,但它不是image但到目前为止,工作组件已经出现并且工作正常但是当我刷新页面时,作业组件消失,并且单击软件链接它以一种奇怪的方式扩展了 url。 component disappers

最佳答案

发生这种情况是因为 want.js 中的这一行

<Route path={`${this.props.location.pathname}/:jobId`} component={Job} />

每次重新加载浏览器时,它都会将该 ID 添加到路径名 /JobPortal/Want/1 的末尾,即 /JobPortal/Want/1/1/JobPortal/Want/1/1/1 因此您不断更改与路线匹配的路径。

直接改成

<Route path={`${this.props.match.path}/:jobId`} component={Job} />

我也会对您的链接更加明确,因为它也会在每次点击时将自身复制到路径上:

<Link to={`/JobPortal/Want/${job.jobId}`}>
{job.jobTitle}
</Link>

像这样使用 this.props.location.pathname 时要小心,因为它包含您的params

关于javascript - 在React中使用react-router时组件消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60324668/

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