在React js中的屏幕之间导航

时间:2020-11-10 16:37:02

标签: javascript reactjs

我正在尝试实现一个按钮,当单击该按钮时,将其更改为React Js中的另一个屏幕,但是当我尝试使用它时,会给我一个错误:TypeError:无法读取未定义的属性“ push”。我不知道该怎么办我做错了什么?谢谢

import LinkForm from "./LinkForm";
import Links from "./Links";
import { db } from "../firebase";
import { BrowserRouter as Router, Route, useHistory } from "react-router-dom";
import { withRouter } from "react-router";

const Link = (props) => {
  const addOrEditLink = async (linkObject) => {
    await db.collection("links").doc().set(linkObject);
    console.log("New Task Added");
  };
  const history = useHistory();

  return (
      <Router>
          <div>
        <LinkForm addOrEditLink={addOrEditLink} />
        <Route path='/links' component={Links}></Route>
        <button
            onClick={() => {
              props.history.push("/links");
            }}
        >
          Go Home
        </button>
      </div>
      </Router>
      
  );
};

export default withRouter(Link);

2 个答案:

答案 0 :(得分:1)

你有什么道具?尝试

onClick={() => {
              history.push("/links");
            }}

答案 1 :(得分:0)

更改

<button
    onClick={() => {
        props.history.push("/links");
    }}>
    Go Home
</button>

收件人

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


<Link to"/links">
    Go Home
</Link>