react函數式組件(非路由組件)實現路由跳轉


個人理解:

 <Route exact path="/Home" component={Home}/>
 1.路由組件:只有包裹在Route組件里的才能使用`this.props.location`, 2.非路由組件:假如有個需求,是面包屑或者導航組件里需要拿到`this.props.location`(導航組件或者面包屑一般不會包裹在`Route`里吧),那么直接這么寫顯然就不行了。 這個時候`withRouter`修飾一下,就可以這么寫了。

 

useHistory

import { useHistory } from "react-router-dom";

function HomeButton() {
  let history = useHistory();

  function handleClick() {
    history.push("/home");
  }

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  );
}

文檔在這里

 

 

 

 

 

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM