首页 > 其他 > 详细

antd Tabs组件动态加载组件内容

时间:2020-03-27 10:48:28      阅读:687      评论:0      收藏:0      [点我收藏+]

Tabs的TabPane子组件不支持通过属性传入Component,官方示例的TabPane内容也都只有简单的文本。如果需要在TabPane的内容中动态传入组件,可以利用jsx特性、采用封装高阶组件的方法实现,方法如下:

1、高阶组件定义

class ToTabContent extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
       //通过传入的name属性动态得到自己需要注入的组件,MyComponent首字母要大写
        const MyComponent = pages[this.props.name]
        
        return <MyComponent {...this.props} />
    }
}

 

2、state定义

this.state = {
    panes : [{
        key: ‘pageA‘,
        title: ‘页面A‘,
        name: ‘pageA‘
    },{
        key: ‘pageB‘,
        title: ‘页面B‘,
        name: ‘pageB‘
    }]
}

 

3、根据state定义的内容渲染TabPane,TabPane内容为state中指定名字的组件

this.state.panes.map( pane => 
    <TabPane tab={ pane.title } key={ pane.key }>
        <ToTabContent name={pane.name} />
    </TabPane>
)}

 

antd Tabs组件动态加载组件内容

原文:https://www.cnblogs.com/wurijie/p/12579268.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!