我正在尝试建立一个基本的路由器网站,如下所示。我想在“ to”与当前网址路径匹配时向元素添加“ selected”类(以便它可以突出显示活动链接)。
我看不到如何将位置获取到App道具中,因此我可以使用该位置来更改样式。
import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import "./App.scss";
function About() {
return <h2>About</h2>;
}
function Projects() {
return <h2>Projects</h2>;
}
function EmailList() {
return <h2>EmailList</h2>;
}
function App(props) {
console.log(props);
return (
<Router>
<div id="router">
<div id="nav">
<Link id="about-link" className="" to="/">
<div className="circle icon-about">
<span>About</span>
</div>
</Link>
<Link id="projects-link" className="selected" to="/projects/">
<div className="circle icon-projects">
<span>Projects</span>
</div>
</Link>
<Link id="writing-link" to="/writing/">
<div className="circle icon-writing">
<span>Writing</span>
</div>
</Link>
<Link id="mailing-list-link" to="/email-list/">
<div className="circle icon-email-list">
<span> Mailing List</span>
</div>
</Link>
</div>
<div id="content">
<Route path="/" exact component={About} />
<Route path="/projects/" component={Projects} />
<Route path="/email-list/" component={EmailList} />
</div>
</div>
</Router>
);
}
export default App;