单击ReactJs中表行中的链接时如何在其他页面中呈现组件

时间:2018-10-23 08:48:25

标签: javascript reactjs react-router-v4 react-router-dom

我是ReactJs的初学者,我有一个ProjectsList组件,其中包含一个html表。其列之一是链接,单击该链接时,它必须导航到呈现ProjectInfo组件的其他页面。

但是,发生的情况是该组件在表末尾的ProjectsList组件的同一页面中呈现。 单击ProjectsList组件中的链接Show Projects时,将渲染Projects组件。

Projects.component.jsx

import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";
import NewProject from "../NewProject/NewProject.component";
import ProjectsList from "../ProjectsList/ProjectsList.component";
import "./Projects.css";

export default class Projects extends Component {
  render() {
    return (
      <div className="projects-dashboard">
        <nav className="nav-up">
          <ul>
            <li>
              <Link to={"/dashboard/projects/add"}>add project</Link>
            </li>

            <li>
              <Link to={"/dashboard/projects/list"}>show projects</Link>
            </li>
          </ul>
        </nav>
        <main>
          <Route path={"/dashboard/projects/add"} component={NewProject} />
          <Route path={"/dashboard/projects/list"} component={ProjectsList} />
        </main>
      </div>
    );
  }
}

ProjectsList.component.jsx

import React, { Component } from "react";
import axios from "axios";
import { Route, Link } from "react-router-dom";
import "./ProjectsList.css";
import ProjectInfo from "../ProjectInfo/ProjectInfo.component";

export default class ProjectsList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      allProject: []
    }
  }

  componentWillMount() {
    this.fetchAllProjects();
  }

  componentDidMount() {
    document.body.style.overflowY = "auto";
  }

  fetchAllProjects = () => {
    axios.get("/api/projects").then(res => {
      this.setState({ allProject: res.data }, () => { console.log(this.state.allProject); });
    });
  };

  render() {
    const projects = this.state.allProject.map(project => {
      return (
        <tr>
          <td>{project.id}</td>
          <td>{project.title}</td>
          <td>{project.organization_name}</td>
          <td className="project-options">
            <Link to={`/dashboard/projects/list/${project.id}`}>
              <i className="far fa-eye" /> show
            </Link>
          </td>
        </tr>
      );
    });

    return (
      <div>
        <table class="projects-list-table">
          <thead>
            <tr>
              <th>
                <h1>Project ID</h1>
              </th>
              <th>
                <h1>Project Name</h1>
              </th>
              <th>
                <h1>Organization Name</h1>
              </th>
              <th>
                <h1>Options</h1>
              </th>
            </tr>
          </thead>
          <tbody>{projects}</tbody>
        </table>
        <main>
          <Route path={"/dashboard/projects/list/:id"} component={ProjectInfo} />
        </main>
      </div>
    );
  }
}

ProjectInfo.component.jsx

import React, { Component } from 'react';
import "./ProjectInfo.css";

export default class ProjectInfo extends Component {
    render() {
        return (
            <div>
                <h1>Project Info Component</h1>
            </div>
        )
    }
}

2 个答案:

答案 0 :(得分:0)

React路由器将那些组件包括到Route标签所在的指定部分。例如:

SELECT *
FROM system.mutations
WHERE is_done = 0

将返回

<section>
  <h1>Hello</h1>
  <Route path={"/random/path"} component={MyComponent} />
</section>

要解决此问题,您需要将其添加到与PrjectList路由相同的路由级别。

所以改变这个

<section>
  <h1>Hello</h1>
  <MyComponent />
</section>

<main>
  <Route path={"/dashboard/projects/add"} component={NewProject} />
  <Route path={"/dashboard/projects/list"} component={ProjectsList} />
</main>

编辑

最后一条路线上不应有确切的标签

<main>
  <Route path={"/dashboard/projects/add"} component={NewProject} />
  <Route exact path={"/dashboard/projects/list"} component={ProjectsList} />
  <Route exact path={"/dashboard/projects/list/:id"} component={ProjectInfo} />
</main>

答案 1 :(得分:0)

如果您要专门渲染一个组件,这意味着当您单击表中的链接时,您只想渲染一个特定的组件(在您的情况下为ProjectInfo),在这种情况下,您不希望渲染不想呈现Projects组件,则需要在路由器内使用Switch

official docs

  

<Switch>

     

渲染第一个孩子<Route><Redirect>   匹配位置。

     

这与仅使用一堆<Route>有什么不同?

     

<Switch>的独特之处在于它专门呈现一条路由。在   相反,与位置匹配的每个<Route>都会呈现   包括在内。

     

考虑以下代码:

<Route path="/about" component={About}/> 
<Route path="/:user" component={User}/>
<Route component={NoMatch}/>
     

如果URL是/ about,则<About><User><NoMatch>将全部   渲染,因为它们都与路径匹配。这是设计使然,使我们   以多种方式将<Route>组合到我们的应用中,例如边栏和   面包屑,引导程序标签等。

因此,将您的Projects组件更改为以下内容(您已经在导入Switch,因此无需再次导入):

<main>
  <Switch>
    <Route exact path={"/dashboard/projects/add"} component={NewProject} />
    <Route exact path={"/dashboard/projects/list"} component={ProjectsList} />
  </Switch>
</main>