news 2026/5/30 15:48:45

使用Taro实现自定义Tabbar遇到切换页面和高亮不同步问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
使用Taro实现自定义Tabbar遇到切换页面和高亮不同步问题

1. Taro实现自定义Tabbar

平时在开发时会遇到一些Tabbar中间有悬浮按钮的需求,比如这样

这时候就需要自定义底部Tabbar,按照官方文档,我们可以这样操作

1.修改app.config配置

tabBar: { selectedColor: themeVars.nutuiColorPrimary, borderStyle: 'white', custom: true, list: [ { pagePath: 'pages/index/index', text: '首页', iconPath: 'assets/images/home.png', selectedIconPath: 'assets/images/home-s.png', }, { pagePath: 'pages/my/my', text: '我的', iconPath: 'assets/images/user.png', selectedIconPath: 'assets/images/user-s.png' } ] },

2.在src新建目录和文件

3.然后我们在index.tsx中填入我们的组件内容

import { useEffect, useState } from 'react' import { View } from '@tarojs/components'; import { Button, Image } from '@nutui/nutui-react-taro'; import './index.scss' import ThemeProvider from '@/theme-provider/ThemeProvider'; import { themeVars } from '@/theme-provider/theme.config'; import Taro from '@tarojs/taro'; // import { useTabBarStore } from '@/store'; function CustomTabBar() { const [curentIndex, setCurrentIndex] = useState(0) // const {selectTab,setSelectTab} = useTabBarStore() const list = [ { pagePath: '/pages/index/index', text: '首页', iconPath: '/assets/images/home.png', selectedIconPath: '/assets/images/home-s.png', }, { pagePath: '/pages/my/my', text: '我的', iconPath: '/assets/images/user.png', selectedIconPath: '/assets/images/user-s.png' } ] return ( <ThemeProvider> <View className='tab-bar'> <View style={{ display: 'flex', justifyContent: 'space-around', alignItems: 'center', width: '100%',paddingTop: '5px' }}> { list.map((item,index) => ( <View className='tab-bar-item' key={index} onClick={ () => { setCurrentIndex(index) Taro.switchTab({ url: item.pagePath }) // 跳转到指定页面 }} > <Image src={index === curentIndex ? item.selectedIconPath : item.iconPath} /> <View style={{ marginTop: 5, color: index === curentIndex ? themeVars.nutuiColorPrimary : '#999' }}>{item.text}</View> </View>)) } <Button className='middle-btn'>1</Button> </View> </View> </ThemeProvider> ) }

这里要注意list从原来的app.config复制过来的,需要改些目录,比如pagePath在app.config里开头是不需要/,但是这里需要

这里实现底部Tabbar凹陷形状的思路狠简单粗暴,直接拿背景图进行覆盖,然后按钮定位上去,这里就不细讲。 当完成了基本的Tabbar之后是这样的

2. 底部Tabbar切换页面和高亮不同步

意思就是说,当我点击某个tab时,会立即切换页面,但是并不高亮,只有我再次点击这个tab才会高亮,如图,

这个问题其实是因为我们的切换页面后状态会重新执行,这个地方用全局状态就可以解决,我这里之前用了zustand,所以这里还是只用zustand进行全局状态管理

1.新建useTabBarStore.ts文件,填入如下代码

import { create } from "zustand"; interface IuseTabBarStore { selectTab: number; setSelectTab: (selectTab: number) => void; } const useTabBarStore = create<IuseTabBarStore>((set) => ({ selectTab: 0, setSelectTab: (selectTab: number) => set({ selectTab }), })) export default useTabBarStore;

2.然后就完美解决了

import { useEffect, useState } from 'react' import { View , Image} from '@tarojs/components'; import { Button } from '@nutui/nutui-react-taro'; import './index.scss' import ThemeProvider from '@/theme-provider/ThemeProvider'; import { themeVars } from '@/theme-provider/theme.config'; import Taro from '@tarojs/taro'; import { useTabBarStore } from '@/store'; function CustomTabBar() { const {selectTab,setSelectTab} = useTabBarStore() const list = [ { pagePath: '/pages/index/index', text: '首页', iconPath: '/assets/images/home.png', selectedIconPath: '/assets/images/home-s.png', }, { pagePath: '/pages/my/my', text: '我的', iconPath: '/assets/images/user.png', selectedIconPath: '/assets/images/user-s.png' } ] return ( <ThemeProvider> <View className='tab-bar'> <View style={{ display: 'flex', justifyContent: 'space-around', alignItems: 'center', width: '100%',paddingTop: '5px' }}> { list.map((item,index) => ( <View className='tab-bar-item' key={index} onClick={ () => { setSelectTab(index) Taro.switchTab({ url: item.pagePath }) // 跳转到指定页面 }} > <Image src={index === selectTab ? item.selectedIconPath : item.iconPath} style={{ width: 24, height: 24 }}/> <View style={{ marginTop: 5, color: index === selectTab ? themeVars.nutuiColorPrimary : '#999' }}>{item.text}</View> </View>)) } <Button className='middle-btn'>1</Button> </View> </View> </ThemeProvider> ) } export default CustomTabBar

本文章是转载文章,以下是作者

作者:零道
链接:https://juejin.cn/post/7418793629597302834
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/29 21:55:25

PCB布局布线基本原则:一文说清高频信号走线策略

以下是对您提供的技术博文《PCB布局布线基本原则:高频信号走线策略深度技术解析》的 全面润色与专业重构版本 。本次优化严格遵循您的全部要求: ✅ 彻底消除AI痕迹,语言风格贴近资深硬件工程师现场分享口吻 ✅ 所有模块有机融合,摒弃“引言/原理/优势/代码”等刻板结构…

作者头像 李华
网站建设 2026/5/28 21:30:02

ChatGLM-6B效果对比评测:vs Qwen1.5-4B vs Baichuan2-7B 中文任务表现

ChatGLM-6B效果对比评测&#xff1a;vs Qwen1.5-4B vs Baichuan2-7B 中文任务表现 1. 为什么中文任务需要“真懂”的模型&#xff1f; 你有没有试过让一个大模型写一封给客户的正式邮件&#xff0c;结果它用词生硬、逻辑跳脱&#xff0c;甚至把“贵司”错写成“你司”&#x…

作者头像 李华
网站建设 2026/5/28 13:27:35

OFA-VE快速部署:单卡3090/4090环境下OFA-VE轻量化运行方案

OFA-VE快速部署&#xff1a;单卡3090/4090环境下OFA-VE轻量化运行方案 1. 为什么需要轻量化的OFA-VE运行方案 你是不是也遇到过这样的情况&#xff1a;下载了OFA-VE项目&#xff0c;满怀期待地执行启动脚本&#xff0c;结果显存直接爆满&#xff0c;GPU占用率冲到100%&#x…

作者头像 李华
网站建设 2026/5/29 1:51:27

ModbusTCP报文格式说明:通过Wireshark验证协议细节

以下是对您提供的博文《Modbus TCP 报文格式深度解析:基于Wireshark协议栈级验证与工程实践指南》的 全面润色与重构版本 。本次优化严格遵循您的全部要求: ✅ 彻底消除AI生成痕迹,语言自然、专业、有“人味”——像一位深耕工控通信十年的嵌入式老兵在技术博客里娓娓道来…

作者头像 李华
网站建设 2026/5/28 19:30:10

多模态AI的跨界革命:从医疗影像到智能家居的实战解析

多模态AI的跨界革命&#xff1a;从医疗影像到智能家居的实战解析 当医生通过AI系统同时分析CT扫描影像和患者病史文本时&#xff0c;当智能家居系统能理解你的语音指令并识别手势动作时&#xff0c;我们正见证着多模态AI技术带来的产业变革。这种能同时处理文本、图像、音频等…

作者头像 李华
网站建设 2026/5/27 21:51:31

从像素迷宫到赛道边界:八邻域算法在智能车视觉中的艺术与科学

从像素迷宫到赛道边界&#xff1a;八邻域算法在智能车视觉中的艺术与科学 当智能车的摄像头凝视赛道时&#xff0c;它看到的不是我们眼中的连续线条&#xff0c;而是一个由无数像素点构成的数字迷宫。每个像素点就像迷宫中的一个十字路口&#xff0c;周围八个方向都可能隐藏着…

作者头像 李华