React TabBar切换与高亮实现

TabBar切换和高亮

渲染 TabBar.Item

js 复制代码
// TabBar 数据
const tabItems = [
  {
    title: "首页",
    icon: "icon-ind",
    path: "/home",
  },
  {
    title: "找房",
    icon: "icon-findHouse",
    path: "/home/list",
  },
  {
    title: "资讯",
    icon: "icon-infom",
    path: "/home/news",
  },
  {
    title: "我的",
    icon: "icon-my",
    path: "/home/profile",
  },
];
js 复制代码
// 渲染 TabBar.Item
  renderTabBarItem() {
    return tabItems.map((item) => (
      <TabBar.Item
        title={item.title}
        key={item.title}
        // 默认图标
        icon={<i className={`iconfont ${item.icon}`} />}
        // 选中图标
        selectedIcon={<i className={`iconfont ${item.icon}`} />}
        // 是否选中
        selected={this.state.selectedTab === item.path}
        // 点击菜单项时,进行路由切换
        onPress={() => {
          this.setState({
            selectedTab: item.path,
          });

          // 路由切换
          this.props.history.push(item.path);
        }}
      />
    ));
  }

内容通过路由来实现切换渲染

noRenderContent={true}

exact 是 React Router 中 Route 组件的一个属性。

功能: 精确匹配路由路径。

当 exact 属性存在时,只有当访问的路径与 /home 完全匹配时,才会渲染 Index(javascript:void(0)) 组件。如果没有 exact,访问 /home/news、/home/list 等路径时,也会匹配到 /home 路由并渲染 Index 组件。

js 复制代码
render() {
    // console.log(this.props.location.pathname)
    return (
      <div className="home">
        {/* 2.3 渲染子路由 */}
        <Route path="/home/news" component={News} />
        <Route exact path="/home" component={Index} />
        <Route path="/home/list" component={HouseList} />
        <Route path="/home/profile" component={Profile} />
        {/* TabBar */}

        <TabBar tintColor="#21b97a" noRenderContent={true} barTintColor="white">
          {this.renderTabBarItem()}
        </TabBar>
      </div>
    );
  }

思路:在 路由切换 时,也执行 菜单高亮 的逻辑代码

1 添加 componentDidUpdate 钩子函数

2 在钩子函数中判断路由地址是否切换(因为路由的信息是通过 props 传递给组件的,所以,通过比较更新前后的两个props)

3 在路由地址切换时,让 菜单高亮

js 复制代码
// 组件接收到新的props(此处,实际上是路由信息)就会触发该钩子函数
  componentDidUpdate(prevProps) {
    // prevProps 上一次的props,此处也就是上一次的路由信息
    // this.props 当前最新的props,此处也就是最新的路由信息
    // 注意:在该钩子函数中更新状态时,一定要在 条件判断 中进行,否则会造成递归更新的问题
    if (prevProps.location.pathname !== this.props.location.pathname) {
      // 此时,就说明路由发生切换了
      this.setState({
        selectedTab: this.props.location.pathname,
      });
    }
  }

默认选中的TabBar菜单项

js 复制代码
state = {
    // 默认选中的TabBar菜单项
    selectedTab: this.props.location.pathname,
  };

是否选中

js 复制代码
selected={this.state.selectedTab === item.path}

点击菜单项时,进行路由切换

js 复制代码
onPress={() => {
          this.setState({
            selectedTab: item.path,
          });

          // 路由切换
          this.props.history.push(item.path);
        }}

首页路由处理

js 复制代码
import React from "react";
import { BrowserRouter as Router, Route, Redirect } from "react-router-dom";
import Home from "./pages/Home";
import CityList from "./pages/CityList";

function App() {
  return (
    <Router>
      <div className="App">
        <Route
          path="/"
          exact
          render={() => <Redirect to="/home" />}
        />
        <Route path="/home" component={Home} />
        <Route path="/city" component={CityList} />
      </div>
    </Router>
  );
}

export default App;
相关推荐
凤城老人9 分钟前
从 PyQt6 到 Electron:给 Edge TTS 做一个“多角色配音机“的踩坑手记
javascript·typescript·electron
Dovis(誓平步青云)12 分钟前
浇水提醒刚弹出又消失,植物状态别只存一个百分比
开发语言·前端·javascript·pdf·ecmascript·电脑
码艺-Alimjan44 分钟前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_1 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒2 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端
广州华水科技2 小时前
大坝安全监测解决方案:单北斗GNSS形变监测系统应用与维护
前端
yivifu2 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub
weixin_448290253 小时前
书庐开发实战教学
javascript·css·html
默_笙3 小时前
🛴 从散件到整机:DeepAgents 与 Agent 身上预留的那些"插槽"(前置介绍)
前端·javascript
zhangzeyuaaa3 小时前
深入理解 Ruby 运算符:本质、分类、坑点与重载实战
开发语言·前端·ruby