javascript - 使用 React 中的 Ant Design 在选项卡中切换选项卡的按钮?

标签 javascript reactjs jsx antd

希望在选项卡的正文中放置一个按钮,该按钮只会切换到下一个选项卡,反之亦然。我已经玩了几个小时了,找不到办法做到这一点。感谢任何帮助

import { Tabs } from 'antd';

const { TabPane } = Tabs;

function callback(key) {
  console.log(key);
}

const Demo = () => (
  <Tabs defaultActiveKey="1" onChange={callback}>
    <TabPane tab="Tab 1" key="1">
      Content of Tab Pane 1 
      <button type="button">
         Go to tab 2
      </button>
    </TabPane>
    <TabPane tab="Tab 2" key="2">
      Content of Tab Pane 2
    </TabPane>
    <TabPane tab="Tab 3" key="3">
      Content of Tab Pane 3
    </TabPane>
  </Tabs>
);

ReactDOM.render(<Demo />, mountNode);

最佳答案

您需要使用 activeKey 属性并将 activeKey 存储在状态中。

import React from 'react'
import { Tabs } from 'antd';
const { TabPane } = Tabs;

function Demo () {
  const [activeKey, setActiveKey] = React.useState('1')
  const onKeyChange = (key) => setActiveKey(key)
  
  return (
    <Tabs defaultActiveKey="1" activeKey={activeKey} onChange={onKeyChange}>
      <TabPane tab="Tab 1" key="1">
        Content of Tab Pane 1
        <button onClick={() => onKeyChange('2')}>Go to "Tab 2"</button>
      </TabPane>
      <TabPane tab="Tab 2" key="2">
        Content of Tab Pane 2
        <button onClick={() => onKeyChange('1')}>Go to "Tab 1"</button>
      </TabPane>
      <TabPane tab="Tab 3" key="3">
        Content of Tab Pane 3
      </TabPane>
    </Tabs>
  )
}

ReactDOM.render(<Demo />, document.getElementById('root'));

我自己还没有测试过,但它应该能让您知道如何去做。

关于javascript - 使用 React 中的 Ant Design 在选项卡中切换选项卡的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68749462/

相关文章:

javascript - 通过 Google Apps 脚本检索修订文本

javascript - 摆脱本地主机 :3000 URLs for ReactAsync

javascript - 为客户端搜索进行多参数过滤的优雅方法

reactjs - 如何模拟另一个文件中的返回值?

reactjs - react 路由器 dom 和 Material UI : forwardRef issue

javascript - 如何修复意外 token "/"的 ESLint 解析错误?

javascript - 如何在 react 回调中触发 handleSubmit

javascript - 我怎样才能制作这个自定义步进器?

javascript - 根据按钮显示特定元素

reactjs - 我如何在我的组件中使用handleSubmit?