React-使用USEATICE根据URL显示特定内容
试图教自己反应并陷入困境...我似乎无法使用USELOCATION()()基于URL来显示特定页面的内容 - 帮助!
app.js-路由器显示页面点击,是的!
<Router>
<Routes>
<Route exact path="/" element={<Home />} />
<Route path="/project/projectOne" element={<Project />} />
<Route path="/project/projectTwo" element={<Project />} />
</Routes>
</Router>
project.js-项目模板按预期的projectIntro.js提供组件
const Project = () => {
return (
<div className='content-wrapper'>
<Scroll />
<ProjectIntro />
<ProjectContent />
<ProjectGrid />
<Contact />
</div>
); }; export default Project;
- 一个试图为内容提供服务的组件 - 这是我所困的,uselocation()请参见“ the the the Path”,但我不知道该如何根据该路径显示“ ProjectIntrodeTails”。
const projectOne = () => {
<h1 className='project-intro-heading'>Title Here</h1>,
<figure className='project-intro-image'>
<img src={projectImage} alt='placeholder'/>
</figure>
}
const projectTwo = () => {
<h1 className='project-intro-heading'>Title Here</h1>,
<figure className='project-intro-image'>
<img src={projectTwoImage} alt='placeholder' />
</figure>
}
const projectIntroDetails = {
projectOne: {
component: <projectOne />
},
projectTwo: {
component: <projectTwo />
}
}
const ProjectIntro = () => {
const projectPath = useLocation();
console.log(projectPath);
// this is where I need help
// how do I turn the path into seeing details to render the correct content?
const projectIntroDetail = projectIntroDetails[projectPath.pathname.split("/project/")];
return (
<div className='project-intro'>
{projectIntroDetail}
</div>
);
}; export default ProjectIntro;
Trying to teach myself react and stuck on one part... I can't seem to get page specific content to display based on URL using useLocation() -- HELP!
App.js - router displays page on click, yay!
<Router>
<Routes>
<Route exact path="/" element={<Home />} />
<Route path="/project/projectOne" element={<Project />} />
<Route path="/project/projectTwo" element={<Project />} />
</Routes>
</Router>
Project.js - Project template serves up the components as expected
const Project = () => {
return (
<div className='content-wrapper'>
<Scroll />
<ProjectIntro />
<ProjectContent />
<ProjectGrid />
<Contact />
</div>
); }; export default Project;
ProjectIntro.js - A component trying to serve up the content -- this is where I'm stuck, useLocation() see's the path, but I can't figure out how to show the "projectIntroDetails" based on that path.
const projectOne = () => {
<h1 className='project-intro-heading'>Title Here</h1>,
<figure className='project-intro-image'>
<img src={projectImage} alt='placeholder'/>
</figure>
}
const projectTwo = () => {
<h1 className='project-intro-heading'>Title Here</h1>,
<figure className='project-intro-image'>
<img src={projectTwoImage} alt='placeholder' />
</figure>
}
const projectIntroDetails = {
projectOne: {
component: <projectOne />
},
projectTwo: {
component: <projectTwo />
}
}
const ProjectIntro = () => {
const projectPath = useLocation();
console.log(projectPath);
// this is where I need help
// how do I turn the path into seeing details to render the correct content?
const projectIntroDetail = projectIntroDetails[projectPath.pathname.split("/project/")];
return (
<div className='project-intro'>
{projectIntroDetail}
</div>
);
}; export default ProjectIntro;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用带有开关语句的组件来确定要渲染的子组件。此方法使您可以将任何其他道具传递给儿童组件。
如果您不需要
&lt; div className ='project-intro'&gt;
元素,则还可以直接在projectintro
component中直接渲染开关。You can use a component with a switch statement to determine which child component to render. This method allows you to pass any additional props to the child components.
If you don't need the
<div className='project-intro'>
element, you could also render the switch directly inside yourProjectIntro
component.您实际上不需要使用
USELocation
挂钩或PATHNAME
值来处理任何条件渲染逻辑,这就是路由组件的目的。我建议将正确的子项目组件作为要在正确匹配的路线上渲染的道具,或者以更“反应路由器”方式进行操作的路由。
将组件降低为Prop示例:
应用
React-Router-dom 为您的优势。
项目
转换
项目
进入布局组件,并渲染projectOne
和projecttwo
nested路线上的组件。布局路由旨在用于共享常见的UI元素和布局,并将路由内容渲染到插座中。应用程序
You don't really need to use the
useLocation
hook orpathname
value to handle any conditional rendering logic, that's what the routing components are for.I would suggest either passing in the correct sub-project component as a prop to be rendered on the correctly matching route, or refactoring the routes to do this in a more "react router" way.
Passing component down as prop example:
App
Project
Using
react-router-dom
to your advantage.Project
Convert
Project
into a layout component and render theProjectOne
andProjectTwo
components on nested routes. Layout routes are intended to be used to share common UI elements and layout, and render routed content into an outlet.App