news 2026/4/23 15:25:44

使用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/4/12 18:50:35

AI作曲高效工作流:从想法到音频仅需30秒

AI作曲高效工作流&#xff1a;从想法到音频仅需30秒 1. 你的私人AI作曲家&#xff1a;Local AI MusicGen上手即用 你有没有过这样的时刻——正在剪辑一段短视频&#xff0c;突然卡在了配乐环节&#xff1f;想找个“带点赛博朋克感的电子氛围”&#xff0c;翻遍音效库却只看到…

作者头像 李华
网站建设 2026/4/23 13:14:43

HY-Motion 1.0开箱即用:无需conda环境重建,root/build下直接bash启动

HY-Motion 1.0开箱即用&#xff1a;无需conda环境重建&#xff0c;root/build下直接bash启动 1. 为什么这次“动起来”特别不一样&#xff1f; 你有没有试过输入一段文字&#xff0c;等了几分钟&#xff0c;结果生成的动作像卡顿的老电视——关节生硬、转身突兀、走路像拖着脚…

作者头像 李华
网站建设 2026/3/14 12:47:46

非技术人员福音:Qwen3Guard-Gen-WEB安全检测实战

非技术人员福音&#xff1a;Qwen3Guard-Gen-WEB安全检测实战 你有没有遇到过这样的场景&#xff1f; 运营同事发来一段营销文案&#xff0c;问&#xff1a;“这段话发出去会不会违规&#xff1f;” 客服主管拿着用户投诉截图说&#xff1a;“这句话听起来有点别扭&#xff0c;…

作者头像 李华
网站建设 2026/4/23 13:12:45

Open Interpreter视觉识图能力:屏幕内容理解操作指南

Open Interpreter视觉识图能力&#xff1a;屏幕内容理解操作指南 1. 什么是Open Interpreter&#xff1f;——让AI真正“看见”你的屏幕 Open Interpreter 不是一个普通聊天工具&#xff0c;而是一个能听懂你说话、看懂你屏幕、还能动手帮你干活的本地AI助手。它不像云端模型…

作者头像 李华
网站建设 2026/4/23 14:39:02

Z-Image-Turbo一键启动,本地服务快速搭建

Z-Image-Turbo一键启动&#xff0c;本地服务快速搭建 你是否试过下载一个AI图像生成模型&#xff0c;结果卡在环境配置、依赖冲突、端口报错的死循环里&#xff1f;是否反复重启服务、查日志、改配置&#xff0c;只为让那个“127.0.0.1:7860”的地址真正亮起来&#xff1f;Z-I…

作者头像 李华
网站建设 2026/4/18 9:12:23

OFA-VE镜像免配置:支持ARM64/NVIDIA Jetson的边缘部署版本

OFA-VE镜像免配置&#xff1a;支持ARM64/NVIDIA Jetson的边缘部署版本 1. 为什么视觉蕴含在边缘端突然变得重要&#xff1f; 你有没有遇到过这样的场景&#xff1a;工厂质检员用手机拍下电路板照片&#xff0c;想立刻确认“焊点是否完整”&#xff1b;或者农业无人机飞过一片…

作者头像 李华