reactjs - React:如何根据路线更改页脚组件的位置
问题描述
我有一个简单的页脚组件。它用于关于和支持页面。position
在我的 sass中,我已将relative
.
我想position
根据路线改变它。如果是/about
则位置:相对,/support
然后是位置:固定。
有可能实现这一目标吗?
function GeneralFooter() {
return (
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6">
<div class="footer-pad">
<ul class="list-unstyled">
<li>
<a
className="nav-link"
href="*"
target="_blank"
rel="noopener noreferrer"
>
Help
</a>
</li>
</ul>
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6">
<div class="footer-pad">
<ul class="list-unstyled">
<li className="nav-item">
<NavLink to="/about" className="nav-link">
<span>About</span>
</NavLink>
</li>
</ul>
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6">
<div class="footer-pad">
<ul class="list-unstyled">
<li className="nav-item">
<NavLink to="/support" className="nav-link">
<span>Support</span>
</NavLink>
</li>
</ul>
</div>
</div>
</div>
</div>
);
}
解决方案
我不确定您是否正在使用任何库,但如果没有,您可以使用以下代码。
使用style
道具:
function GeneralFooter() {
const location = useLocation();
const pathName = location.pathname;
return (
<div
className="container"
style={{
position: pathName === '/about' ? 'relative' : pathName === '/support' ? 'fixed' : 'inherit'
}}
>
...
使用className
道具
.footer--about {
position: relative;
}
.footer--support {
position: fixed;
}
function GeneralFooter() {
const location = useLocation();
const pathName = location.pathname;
const extraClassName = pathName === '/about' ? 'footer--about' : pathName === '/support' ? 'footer--support' : '';
return (
<div
className={`container ${extraClassName}`}
>
...
使用classNames依赖项:
function GeneralFooter() {
const location = useLocation();
const rootClassName = classNames('container', {
'footer-about': location.pathname === '/about',
'footer-support': location.pathname === '/support',
});
return (
<div className={rootClassName}>
...
推荐阅读
- windows - 在 terraform 中解密 Windows 密码
- python - 用 Python 计算文本中的单词(偶数倍数)
- excel - 使用 VBA 在工作表之间复制单元格
- batch-file - 如何给 .bat 代码行一个时间限制?
- swift - UITableView 自定义单元格高度未正确管理 swift 中的动态约束
- json - Azure pipelines -Add new element in json array VSTS pipelines (Appsettings.json)
- php - IOS 推送通知横幅
- r - 结合ggmap和ggplot在r中创建动画
- apache - Apache代理不同的上下文路径cookie问题
- java - 参数值 [{}] 与预期类型不匹配 [java.lang.String (n/a)]