Home > Web Front-end > JS Tutorial > body text

Examples of using higher-order components in React

不言
Release: 2018-11-15 17:23:49
forward
2203 people have browsed it

The content of this article is about the use examples of high-level components in React. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Higher-Order Components

  • HOC is not a standard API for React.

  • HOC is a function.

  • HOC returns a Component.

Example:

const EnhancedComponent = higherOrderComponent(WrappedComponent);
Copy after login

Usage scenarios

Code reuse

Similar to the mixin before React version 0.15.

HOC can be used when multiple components use a piece of code or the same method.

Example:

import React, { PureComponent } from 'react';

const Common = (WrapComponent) => {
  return (
    <div>
      <h1>Title</h1>
      <WrapComponent />
    </div>
  );
};

const Header = () => <div>Header</div>;
const Footer = () => <div>Footer</div>;

export default class extends PureComponent {
  render() {
    return (
      <div>
        <div>
          <p>Header Component</p>
          {Common(Header)}
        </div>
        <hr />
        <div>
          <p>Footer Component</p>
          {Common(Footer)}
        </div>
      </div>
    );
  }
}
Copy after login

Online example

Abstract state and change props

can be wrapped by WrappedComponent The public state of the components is abstracted.

You can pass props for modification, addition, etc. through the component wrapped by WrappedComponent.

Example:

const HOComp = (WrappedComponent) => {
  return class extends React.Component {
    constructor(props) {
      super(props);
      this.state = {name: ''};
    }

    componentDidMount() {
      this.setState({name: WrappedComponent.displayName || WrappedComponent.name || 'Component';});
    }

    return <WrappedComponent name={this.state.name} {...this.props}/>
  }
}
Copy after login

Xuanran hijacking

Conditional Xuanran. Return the content in Xuanran based on props or state conditions.

Example:

const HOComp = (WrappedComponent) => {
  return class Enhancer extends WrappedComponent {
    render() {
      if (this.props.loggedIn) {
        return super.render()
      } else {
        return null
      }
    }
  }
}
Copy after login

Reverse inheritance

The returned higher-order component class (Enhancer) inherits WrappedComponent.

Example:

const EnchanceComponent = (WrappedCompopnent) => {
  return class extends WrappedCompopnent {
    constructor(props) {
      super(props);
      this.state = { error: '' };
    }
    componentDidMount() {
      /*do something*/
      super.componentDidMount();
    }
    render() {
      if (this.state.error) {
        return <p>{this.state.error}</p>;
      } else {
        return super.render();
      }
    }
  }
};
Copy after login

Online example

The above is the detailed content of Examples of using higher-order components in React. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template