react router v6 類組件獲取跳轉


import { Route, Routes, useNavigate } from "react-router-dom";


export const withNavigation = (Component) => {
  return (props) => <Component {...props} navigate={useNavigate()} />;
};

class Home extends Component {
  state = {
    activeKey: "/index",
    tabs: [
      {
        key: "/index",
        title: "首頁",
        icon: <AppOutline />,
      },
      {
        key: "/list",
        title: "找房",
        icon: <UnorderedListOutline />,
      },
      {
        key: "/news",
        title: "咨詢",
        icon: <MessageOutline />,
      },
      {
        key: "/profile",
        title: "我的",
        icon: <UserOutline />,
      },
    ],
  };
  setActiveKey = (e) => {
    let path = "/home" + e;
    this.setState({
      activeKey: e,
    });
    //跳轉  不能用this.props.history.push()報錯
    this.props.navigate(path);
  };

  setActiveKey(params) {}
  render() {
    return (
      <div className="homeCss">
          {/* 路由規則 */}
        <Routes>
          <Route path="/*" element={<Index />}></Route>
          <Route path="/home/news" element={<News />}></Route>
          <Route path="/home/list" element={<HouseList />}></Route>
          <Route path="/home/profile" element={<Profile />}></Route>
        </Routes>
        {/* 路由規則 */}
        <div className="tabbar">
          <TabBar activeKey={this.state.activeKey} onChange={this.setActiveKey}>
            {this.state.tabs.map((item) => (
              <TabBar.Item key={item.key} icon={item.icon} title={item.title} />
            ))}
          </TabBar>
        </div>
      </div>
    );
  }
}
//使用 withNavigation包裹類組件名稱
export default withNavigation(Home);

 


免責聲明!

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



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