繁体   English   中英

在 React.js 中实现路由器来更改页面

[英]implementing Router to change pages in React.js

我正在制作一个我拥有的 react.js 购物清单应用程序,以便您输入值并将它们打印到表格中。 这是我第一次使用 React,我想将我的表放在一个单独的页面上我知道 ROUTER 方法,但在实现它时遇到了麻烦。

这是我的代码说明:我还没有添加任何东西来在页面之间导航,但我希望打印按钮从 App.js 导航到 Details.js

import NavBar from "./components/navbar";
import "./App.css";
import Counters from "./components/counters";
import Details from "./Details";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";

class App extends Component {
  state = {
    counters: [
      //example of objects in array
      /*{ id: 1, value: 2, text: "hi" },*/
    ],
  };
  constructor(props) {
    super(props);
    console.log("app- constructor");
  }

  componentDidMount() {
    console.log("app- mouneted");
  }

  handleIncrement = (counter) => {
    const counters = [...this.state.counters];
    const index = counters.indexOf(counter);
    counters[index] = { ...counter };
    counters[index].value++;
    this.setState({ counters });
  };

  handleDecrement = (counter) => {
    const counters = [...this.state.counters];
    const index = counters.indexOf(counter);
    counters[index] = { ...counter };
    counters[index].value--;
    this.setState({ counters });
  };

  handleReset = () => {
    const counters = this.state.counters.map((c) => {
      c.value = 0;
      return c;
    });
    this.setState({ counters });
  };

  handleName = () => {
    var name = prompt("name");
    return name;
  };

  handleCreate = () => {
    const create = this.state.counters.length + 1;
    const counter = this.state.counters.push({
      id: create,
      value: 0,
      text: this.handleName(),
    });
    this.setState({ counter });
  };

  handleDelete = (counterId) => {
    const counters = this.state.counters.filter((c) => c.id !== counterId);
    this.setState({ counters });
  };

  handleInfo = () => {
    let x;
    //loops through all tems and adds the to the list
    for (x = 0; x <= this.state.counters.length; x++) {
      //breaks loop if x is == to counters array
      if (this.state.counters.length == x) {
        break;
      }
      const info = this.state.counters[x]["text"];
      const quantity = this.state.counters[x]["value"];
      console.log(info, quantity);

      //creates rows based on input
      var table = document.getElementById("myList");
      var row = table.insertRow(1);
      var itemCell = row.insertCell(0);
      var quantityCell = row.insertCell(1);
      itemCell.innerHTML = info;
      quantityCell.innerHTML = quantity;
    }
    //calls the print function
    return <a href="http://localhost:3000/Details"></a>;
  };

  render() {
    console.log("rendered");
    return (
      <React.Fragment>
        <NavBar
          totalCounters={this.state.counters.filter((c) => c.value > 0).length}
        />

        <main className="container">
          <Counters
            counters={this.state.counters}
            onReset={this.handleReset}
            onCreate={this.handleCreate}
            onIncrement={this.handleIncrement}
            onDecrement={this.handleDecrement}
            onDelete={this.handleDelete}
            onInfo={this.handleInfo}
          />
        </main>

        <div>
          <table bordered id="myList">
            <tr>
              <th>Items</th>
              <th>Quantity</th>
            </tr>
          </table>
        </div>
        <style>
          {`
          th{
            border: 2px solid black;
            padding: 2px;
            min-width: 100px;
            font-size: 22px;
            font-style: bold;
          }
          
          td{
            border: 2px solid black;
            padding: 2px;
            min-width: 100px;
            font-size: 18px;
          }

        
          `}
        </style>
      </React.Fragment>
    );
  }
}

export default App;

这是我的应用程序的样子,但我希望表格位于“详细信息”页面上任何帮助表示赞赏

在此处输入图像描述

Router不是一种方法,而是一个包装应用程序的组件。 Router内部, Switch组件包装了Route的几个实例,这些实例又包装了在命中该路由时要呈现的组件。 最后, Link组件用于构建指向各种Route实例的链接。 总而言之,它看起来像这样:

import { 
    BrowserRouter as Router, 
    Switch, 
    Route,
    Link
} from "react-router-dom";

<Router>
    <Switch>

        <Route exact path="/">
            <div>

                ...insert home page here...

                <Link to="/details">Click here to go to details page!</Link>

            </div>
        </Route>

        <Route path="/details">
            ...details page/component here...
        </Route>

    </Switch>
</Router>

有关更多信息,请参阅React Router 教程

暂无
暂无

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

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