简体   繁体   English

React-router重定向到首页

[英]React-router redirect to Homepage

I have such multiple routes for Career, Project and Apply Job. 我有多种职业,项目和应聘工作路线。

But I want to speak about Project only I have a lot of projects data fetched from backend 但是我只想谈谈Project,我有很多从后端获取的Project数据

In one single Project I have information about this project called ProjectDetails (this is the component that renders a single project) so fe http://localhost:4000/#/projects/913 with ID of 913 has info about fetched project with ID 913 and so on. 在一个项目中,我有一个名为ProjectDetails的项目信息(这是呈现单个项目的组件),因此http:// localhost:4000 /#/ projects / 913 (ID为913)具有有关ID 913的已提取项目的信息等等。

What I am trying to achieve is to Redirect user to http://localhost:4000/ ( homepage) if he types in url something that do not exist, fe http://localhost:4000/#/projects/someID (someID is never being fetched from backend) 我要实现的目标是,如果用户在url中输入不存在的内容,例如http:// localhost:4000 /#/ projects / someID (someID为, 则将用户重定向到http:// localhost:4000 / (主页)永远不会从后端获取)

Any thoughts or advices how I can achieve this with Redirect component of React-router? 任何想法或建议我如何使用React-router的Redirect组件来实现?

My ProjectDetails component looks like this: 我的ProjectDetails组件如下所示:

let ProjectDetails = ({ projects, match }) => {
  if (!projects.length) return false;
  const project = projects.find(item => item.Id == match.params.id);
  return (
    <Element name='Projects'>
      <SectionActiveTile match={match} name={project.Title}>
        <div className='project_details_content'>
          <div className="project_images">
            <LazyImg src={`http://mywebsite.co/media/projects/${project.ImageURL}`} alt={`${project.Title} image`} />
            <div className="project_icons">
              {!!project.WebSiteURL &&
                <a target='_blank' href={project.WebSiteURL}>
                  <LazyImg src={webImg} alt="Web img" />
                </a>
              }
              {!!project.iTunesStoreURL &&
                <a target='_blank' href={project.iTunesStoreURL}>
                  <LazyImg src={appStoreImg} alt="AppStore img" />
                </a>
              }
              {!!project.GooglePlayURL &&
                <a target='_blank' href={project.GooglePlayURL}>
                  <LazyImg src={googlePlayImg} alt="Google Play img" />
                </a>
              }
            </div>
          </div>
          <div className="project_description">
            <h4>Customer:</h4>
            <p>{project.Customer}</p>
            <h4>Project Facts:</h4>
            <p>{project.ProjectFacts}</p>
            <h4>Technologies:</h4>
            <p>{project.Technologies}</p>
          </div>
        </div>
      </SectionActiveTile>
    </Element>
  );
}; 

Projects info is passed down from parent component to ProjectDetails 项目信息从父组件传递到ProjectDetails

UPDATED 更新

Here are my routes: 这是我的路线:

    <Switch>
      <Route path='/projects/:id' component={ProjectDetails} />
      <Route path='/career/:id' component={CareerDetails} />
      <Route path='/apply-for-job' render={(props) => (
        <ModalWindow
          {...props}
          modalHeader='Apply form'>
          <ApplyForm history={props.history} />
        </ModalWindow>
      )} />
      <Route path='/' component={withScrollPreservation(LandingPage, Footer)} />     
    </Switch>

You just need to use Redirect component from react-router . 您只需要使用react-router Redirect组件。

You could modify ProjectDetails like this. 您可以像这样修改ProjectDetails

let ProjectDetails = ({ projects, match }) => {
  let HomeURL = ""; //add your homeURL here
  if (!projects.length) {
      return (<Redirect to = {HomeURL} />);
  }
  const project = projects.find(item => item.Id == match.params.id);
  if(project === undefined) {
      return (<Redirect to = {HomeURL} />);
  }
  return (
      //your code as is
  );
}

You can handle no match in the following way 您可以通过以下方式处理不匹配的问题

     <Switch>
          <Route path='/projects/:id' component={ProjectDetails} />
          <Route path='/career/:id' component={CareerDetails} />
          <Route path='/apply-for-job' render={(props) => (
            <ModalWindow
              {...props}
              modalHeader='Apply form'>
              <ApplyForm history={props.history} />
            </ModalWindow>
          )} />
          <Route path='/' component={withScrollPreservation(LandingPage, Footer)} />     
         <Route component={HomePage}/> 
        </Switch>

Please refer the documentation for more details 请参阅文档以获取更多详细信息

You can push new route if you don't find project. 如果找不到项目,则可以推新路线。 For your reference Programmatically navigate using react router 供参考, 以使用React Router进行编程导航

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM