You need to enable JavaScript to run this app. React Material UI Card功能组件实现路由跳转及页面叠加问题修复
优惠活动
大模型
产品
解决方案
定价
更多

React Material UI Card功能组件实现路由跳转及页面叠加问题修复

问题根源拆解
  • 你遇到的页面堆叠、点击无跳转的问题,是两处配置错误叠加导致的:
    • 路由配置没有把UMainPage纳入路由管控体系:当前根路由/只写了硬编码的"Homepage"文本,没有将UMainPage作为路由组件挂载,实际运行时UMainPage是被直接渲染在路由Switch组件外部的,会一直显示在页面上,不受路由切换控制。
    • 当页面路径匹配/rentPage规则时,Switch组件会正常渲染URentPage,而一直存在的UMainPage不会被自动卸载,两个页面自然就上下堆叠在了一起。
    • 另外你在UMainPage中无意义引入了URentPage组件,没有实际渲染逻辑也增加了不必要的打包体积。
具体修正实现步骤

第一步:修正App.tsx的路由配置

将根路由对应的渲染内容替换为UMainPage组件,让UMainPage完全受路由控制,路径不匹配时会自动卸载:

import { BrowserRouter, Switch, Route } from 'react-router-dom';
import UMainPage from './UMainPage';
import URentPage from './URentPage';

class App extends Component {
  render(){
    return (
      <BrowserRouter>
        <Switch>
          <Route exact path='/' component={UMainPage}/>
          <Route path='/rentPage' component={URentPage}/>
        </Switch>
      </BrowserRouter>
    );
  }
}

注意:记得删除UMainPage里多余的import RentPage from './URentPage'行,避免重复引入无关组件。

第二步:给卡片组件添加跳转逻辑

结合你使用的Material UI组件,有两种适配的实现方式,根据业务需求选择即可:

方式1:无前置逻辑,直接用路由Link实现跳转

适合只需要点击卡片跳转、不需要额外执行操作的场景,代码侵入性最低:

  • 从react-router-dom引入Link组件
  • 给Material UI的CardActionArea组件设置component={Link}和to="/rentPage"属性,让可点击区域直接变成路由链接:
// UMainPage.tsx 代码参考
import { Link } from 'react-router-dom';

<Card className={classes.venueCard}>
    <CardActionArea component={Link} to="/rentPage">
        {/* 原有CardMedia、CardContent内容保持不变 */}
        <CardMedia
            className={classes.media}
            image = {card1}
            title="气膜馆"
        />
        <CardContent>
            <Typography gutterBottom variant="h5" component="h2">
                清华大学气膜馆
            </Typography>
            <Typography variant="body2" color="textSecondary" component="p">
                球类: 羽球场 乒乓球
            </Typography>
        </CardContent>
    </CardActionArea>
    {/* 原有CardActions部分保持不变 */}
    <CardActions className="classes.venueCardActions">
        <PeopleIcon style={{fill:"#27496B", marginLeft:"5px", height: 35, width: 35}}/>
        <Typography variant="body2" color="inherit">
            300人
        </Typography>
        <SportsIcon style={{fill:"#27496B", marginLeft:"50px",height: 35, width: 35}}/> 
        <Typography variant="body2" color="inherit">
            10个羽球场 10个乒乓球场
        </Typography>
    </CardActions>
</Card>

这种方式会自动拦截链接的默认页面刷新行为,跳转是纯前端路由切换,不会刷新整站。

方式2:需要前置业务逻辑时,结合onClick事件手动触发跳转

适合点击卡片时需要先执行埋点、权限校验、表单确认等操作再跳转的场景,用react-router提供的useNavigate hook实现:

// UMainPage.tsx 代码参考(函数组件写法)
import { useNavigate } from 'react-router-dom';

export default function UMainPage() {
  const navigate = useNavigate();

  // 卡片点击事件处理
  const handleCardClick = () => {
    // 这里写你需要在跳转前执行的任意业务逻辑
    navigate('/rentPage');
  }

  return (
    <div>
      <Card className={classes.venueCard}>
          {/* 给CardActionArea绑定onClick事件 */}
          <CardActionArea onClick={handleCardClick}>
              {/* 内部的CardMedia、CardContent保持原有写法不变 */}
              ...
          </CardActionArea>
          {/* CardActions保持原有写法不变 */}
          ...
      </Card>
    </div>
  )
}
效果验证

配置完成后,启动项目访问localhost:3000,页面只会渲染UMainPage的内容,点击卡片后会无刷新跳转到/rentPage路径,页面自动替换为URentPage的测试标题,不会再出现两个页面堆叠的情况。


内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.16 09:06:04