--- order: 11 title: 自定义新增页签触发器 --- 隐藏默认的页签增加图标,给自定义触发器绑定事件。 ````jsx import { Tabs, Button } from 'antd'; const TabPane = Tabs.TabPane; const Demo = React.createClass({ getInitialState() { this.newTabIndex = 0; const panes = [ 选项卡一内容, 选项卡二内容, ]; return { activeKey: panes[0].key, panes, }; }, onChange(activeKey) { this.setState({ activeKey }); }, onEdit(targetKey, action) { this[action](targetKey); }, add() { const panes = this.state.panes; const activeKey = `newTab${this.newTabIndex++}`; panes.push(新页面); this.setState({ panes, activeKey }); }, remove(targetKey) { let activeKey = this.state.activeKey; let lastIndex; this.state.panes.forEach((pane, i) => { if (pane.key === targetKey) { lastIndex = i - 1; } }); const panes = this.state.panes.filter(pane => pane.key !== targetKey); if (lastIndex >= 0 && activeKey === targetKey) { activeKey = panes[lastIndex].key; } this.setState({ panes, activeKey }); }, render() { return (
{this.state.panes}
); }, }); ReactDOM.render(, mountNode); ````