账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    对于自定义高复用,可扩展React组件的实现
    33
    0
    • 对于自定义灵活的可扩展React组件形式,如一个Tab组件:
    <Tabs defaultActiveIndex="1" className="tabs-bar">
        <TabNav>
           <TabHead order='1'><div className='Tabs-1'>Tabs-1</div></TabHead>
           <TabHead order='2'>Tabs-2</TabHead>
           <TabHead order='3'>Tabs-3</TabHead>
        </TabNav>
        <TabContent>
           <TabPane order='1'>1</TabPane>
           <TabPane order='2'><div>tab-2</div></TabPane>
           <TabPane order='3'>3</TabPane>
         </TabContent>
    </Tabs>
    • 对于这种形式的组件,应该如何方便的实现?
    • 如果直接在父组件的render返回内容中使用this.props.children,便会出现如何传递数据的问题;
    • 尝试过使用React.Children和React.cloneElement配合对子组件进行克隆传递数据,如在TabContent组件中通过定于一个getTabPanes()对子组件进行克隆传递数据并返回:
    import React, { Component } from 'react';
    import classnames from 'classnames';
    import PropTypes from 'prop-types';
    
    class TabContent extends Component {
        static propTypes = {
            classPrefix: PropTypes.string,
            activeIndex: PropTypes.string,
        }
    
        getTabPanes() {
            const { activeIndex, children } = this.props;
    
            return React.Children.map(children, (child) => {
                if (!child) { return; }
    
                const order = child.props.order;
                const isActive = activeIndex === order;
    
                return React.cloneElement(child, {
                    isActive,
                    ...this.props,
                    children: child.props.children,
                    key: `tabpane-${order}`,
                })
            })
        }
    
    
        render() {
            const { classPrefix } = this.props;
    
            const classes = classnames({
                [`${classPrefix}-content`]: true,
            })
    
            return (
                <div className = {classes}>
                    { this.getTabPanes() }
                </div>
            )
        }
    }
    export default TabContent;
    • 对于这种形式,感觉逻辑有点复杂,不怎么清晰,很容易出现问题,而且得平白无故的多克隆出一个组件(这样每次更新都会从新克隆出一个组件导致从新渲染)会不会对性能有比较大的影响?
    • 大家一般是如何实现这种形式的组件?使用context传数据吗?还是其他方法?

    求指点、求指点。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 黑夜执笔 普通会员 1楼

      在React中,自定义高复用,可扩展组件的实现可以通过以下步骤:

      1. 创建组件类:首先,你需要创建一个React组件类。这将定义你的组件的结构和行为。

      jsx class MyComponent extends React.Component { render() { return ( <div> {this.props.message} </div> ); } }

      1. 添加事件监听器:在组件的render方法中添加事件监听器,以便在组件的状态发生变化时触发。这可以通过React的useState或useEffect Hook来实现。

      ```jsx class MyComponent extends React.Component { constructor(props) { super(props); this.state = { message: 'Hello, World!' }; this.handleMessage = this.handleMessage.bind(this); }

      handleMessage(event) { this.setState({ message: event.target.innerText }); }

      render() { return (

      {this.state.message}
      ); } } ```

      1. 使用高复用:在你需要显示某个信息的组件中,只需使用你的组件实例,就可以在其他组件中显示这个信息。

      jsx class MyComponent extends React.Component { render() { return ( <div> <MyComponent message="Hello, World!" /> </div> ); } }

      1. 可扩展性:由于组件是高度可重用的,因此你可以在任何需要显示消息的组件中使用它,而无需重复编写代码。例如,你可以在其他组件中创建一个子组件,用于显示消息。

      jsx class MessageComponent extends React.Component { render() { return ( <div> <MyComponent message="Hello, World!" /> </div> ); } }

      这样,你就可以通过使用自定义高复用,可扩展的React组件来创建复杂的用户界面。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部