antd-React Popover 点击空白不隐藏

文章讲述了在编程中遇到点击事件覆盖默认事件,导致按钮点击不生效的问题。作者提出了一个解决方案,即通过添加焦点事件并在失去焦点时改变状态来解决。示例代码展示了使用useStateHook和Blur事件处理函数来控制Popover组件的可见性。
摘要由CSDN通过智能技术生成

1.问题原因:自己写的点击事件把默认事件覆盖掉了,所以点击会不生效

2.解决方案:给按钮在添加一个焦点事件即可,当失去焦点的时候取反
3.代码如下

  const [closeVisible, setCloseVisible] = useState(false);
              <Popover content={content} placement="bottom" trigger="click" visible={closeVisible}>
                <Button
                  type={"primary"}
                  onBlur={()=>setCloseVisible(!closeVisible)}
                  onClick={()=>setCloseVisible(!closeVisible)}
                  >新建
                </Button>
              </Popover>

4.效果展示

在这里插入图片描述

你可以使用antd-mobile和react-router-dom来实现tabs功能。首先,你需要安装antd-mobile和react-router-dom依赖包。 ```shell npm install antd-mobile react-router-dom ``` 接下来,你可以创建一个Tabs组件,并在其使用antd-mobile的TabBar组件来实现tabs布局。然后,使用react-router-dom的Route组件来定义每个tab对应的页面。 ```jsx import React from 'react'; import { TabBar } from 'antd-mobile'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const Tabs = () => { const tabItems = [ { title: 'Tab 1', path: '/tab1', component: Tab1 }, { title: 'Tab 2', path: '/tab2', component: Tab2 }, { title: 'Tab 3', path: '/tab3', component: Tab3 }, ]; const renderTabs = () => { return tabItems.map((item) => ( <TabBar.Item key={item.path} title={item.title} icon={<div />} selectedIcon={<div />} selected={window.location.pathname === item.path} onPress={() => { window.location.href = item.path; }} /> )); }; return ( <Router> <div style={{ position: 'fixed', width: '100%', bottom: 0 }}> <TabBar>{renderTabs()}</TabBar> </div> {/* Define routes */} {tabItems.map((item) => ( <Route key={item.path} path={item.path} component={item.component} /> ))} </Router> ); }; const Tab1 = () => <div>Tab 1 content</div>; const Tab2 = () => <div>Tab 2 content</div>; const Tab3 = () => <div>Tab 3 content</div>; export default Tabs; ``` 在上面的代码,我们定义了三个tab,分别对应不同的路径和组件。通过点击tab,我们可以切换显示不同的内容。你可以根据自己的需求修改tab的数量、标题、路径和对应的组件。 希望这可以帮助你实现antd-mobile和react-router-dom的tabs功能!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值