首页 > 其他分享 >React进阶篇——十、高阶组件使用场景

React进阶篇——十、高阶组件使用场景

时间:2022-10-27 17:13:32浏览次数:52  
标签:return WrappedComponent value React 进阶篇 props 组件 高阶

十、高阶组件使用场景

  • 操纵props

在被包装组件接收props前,高阶组件可以先拦截到props,对props执行增加、删除或修改的操作,然后将处理后的props再传递给被包装组件,上一篇的例子就属于这种情况。

  • 通过ref访问组件实例
function withPersistentData(WrappedComponent) {
    return class extends Component {
        constructor(props) {
            super(props);
            this.someMethod = this.someMethod.bind(this);
        }

        someMethod() {
            this.wrappedInstance.someMethodInWrappedComponent();
        }
        render() {
            return <WrappedComponent ref={(instance) => {
                this.wrappedInstance = instance
            }} {...this.props} />
        }
    }
}

高阶组件通过this.wrappedInstance保存WrappedComponent实例的引用,在someMethod中,通过this.wrappedInstance调用WrappedComponent中的方法。

  • 组件状态提升

高阶组件可以通过 将被包装组件的状态及相应的状态处理方法提升到高阶组件自身内部,从而实现被包装组件的无状态化(无状态组件更容易被复用)。

function withPersistentData(WrappedComponent) {
    return class extends Component {
        constructor(props) {
            super(props);
            this.state = {
                value: ''
            }
            this.handleValueChange = this.handleValueChange.bind(this);
        }

        handleValueChange(event) {
            this.setState({
                value: event.target.value
            })
        }
        render() {
            const newProps = {
                controlledProps: {
                    value: this.state.value,
                    onChange: this.handleValueChange
                }
            }
            return <WrappedComponent {...this.props} {...newProps} />
        }
    }
}

class SimpleControlledComponent extends Component {
    render() {
        return (
            <input name="simple" {...this.props.controlledProps}/>
        )
    }
}
const Todo = withPersistentData(SimpleControlledComponent)
export default Todo;

这个例子把受控组件value属性用到的状态和处理value变化的回调函数都提升到高阶组件中。SimpleControlledComponent变成无状态组件,状态由高阶组件维护。

  • 用其他元素包装组件

常用于为WrappedComponent增加布局或修改样式

function withPersistentData(WrappedComponent) {
    return class extends Component {
        render() {
            return (
                <div style={{background: 'red'}}>
                    <WrappedComponent {...this.props} />
                </div>
            )
        }
    }
}

 

标签:return,WrappedComponent,value,React,进阶篇,props,组件,高阶
From: https://www.cnblogs.com/sxww-zyt/p/16832903.html

相关文章

  • react组件深度解读
    五、React核心是组件在React中,我们使用组件(有状态、可组合、可重用)来描述UI。在任何编程语言中,你都可以将组件视为简单的函数。React组件也一样,它的输入是props......
  • React实现一个简易版Swiper
    背景最近在公司内部进行一个引导配置系统的开发中,需要实现一个多图轮播的功能。到这时很多同学会说了,“那你直接用swiper不就好了吗?”。但其实是,因为所有引导的展示都是作......
  • React高级特性之Render Props
    renderprop是一个技术概念。它指的是使用值为function类型的prop来实现Reactcomponent之间的代码共享。如果一个组件有一个render属性,并且这个render属性的值为一个返......
  • React高级特性之Context
    Context提供了一种不需要手动地通过props来层层传递的方式来传递数据。正文在典型的React应用中,数据是通过props,自上而下地传递给子组件的。但是对于被大量组件使用的......
  • 项目实战:在线报价采购系统(React +SpreadJS+Echarts)
    小伙伴们对采购系统肯定不陌生,小到出差路费、部门物资采购;大到生产计划、原料成本预估都会涉及到该系统。管理人员可以通过采购系统减少管理成本,说是管理利器毫不过分,对于......
  • 前端一面高频react面试题(持续更新中)
    如何避免组件的重新渲染?React中最常见的问题之一是组件不必要地重新渲染。React提供了两个方法,在这些情况下非常有用:React.memo():这可以防止不必要地重新渲染函数组......
  • 老生常谈React的diff算法原理-面试版
    第一次发文章notonly(虽然)版式可能有点烂butalso(但是)最后赋有手稿研究finally看完他你有收获diff算法:对于update的组件,他会将当前组件与该组件在上次更新是对应的......
  • ReactJS单页面应用之项目搭建
    初衷因接手的项目前端采用reactjs+antd,为把控项目中的各个细节,所以想做一些整理,以免后期遗忘。创建及启动项目#全局安装create-react-app#如果曾经安装过,可先移除:n......
  • react实战笔记134:redux简介
      ......
  • react实战笔记136:HelloRedux的实现
     第一步创建函数 第二步设置初始值 第三布直接赋值......