This article mainly gives you an in-depth understanding of React high-order components. I hope you will have a clearer understanding of React high-order components.
1. In React, higher-order component (HOC) is an advanced technology for reusing component logic. HOC is not part of the React API. A HOC is a function that takes a component and returns a new component. In React, components are the basic unit of code reuse.
2. In order to explain HOCs, take the following two examples
The CommentList component will render a comments list, and the data in the list comes from the outside.
class CommentList extends React.Component { constructor() { super(); this.handleChange = this.handleChange.bind(this); this.state = { // "DataSource" is some global data source comments: DataSource.getComments() }; } componentDidMount() { // Subscribe to changes DataSource.addChangeListener(this.handleChange); } componentWillUnmount() { // Clean up listener DataSource.removeChangeListener(this.handleChange); } handleChange() { // Update component state whenever the data source changes this.setState({ comments: DataSource.getComments() }); } render() { return ( <p> {this.state.comments.map((comment) => ( <Comment comment={comment} key={comment.id} /> ))} </p> ); } }
Next is the BlogPost component, which is used to display a blog message
class BlogPost extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.state = { blogPost: DataSource.getBlogPost(props.id) }; } componentDidMount() { DataSource.addChangeListener(this.handleChange); } componentWillUnmount() { DataSource.removeChangeListener(this.handleChange); } handleChange() { this.setState({ blogPost: DataSource.getBlogPost(this.props.id) }); } render() { return <TextBlock text={this.state.blogPost} />; } }
These two components are different, they call different methods of DataSource, and their output are different, but most of their implementations are the same:
1. After the loading is completed, a change listener is added to the DataSource
2. When the data source changes, inside the listener Call setState
3. After uninstalling, remove the change listener
It is conceivable that in large applications, the same pattern of accessing DataSource and calling setState will happen again and again. We want to abstract this process so that we only define this logic in one place and then share it across multiple components.
Next we write a function that creates a component. This function accepts two parameters, one of which is the component and the other is the function. The withSubscription function is called below
const CommentListWithSubscription = withSubscription( CommentList, (DataSource) => DataSource.getComments() ); const BlogPostWithSubscription = withSubscription( BlogPost, (DataSource, props) => DataSource.getBlogPost(props.id) );
The first parameter passed when calling withSubscription is the wrapped component, and the second parameter is a function, which is used to retrieve data.
When CommentListWithSubscription and BlogPostWithSubscription are rendered, CommentList and BlogPost will accept a prop called data, which stores the data currently retrieved from the DataSource. The withSubscription code is as follows:
// This function takes a component... function withSubscription(WrappedComponent, selectData) { // ...and returns another component... return class extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.state = { data: selectData(DataSource, props) }; } componentDidMount() { // ... that takes care of the subscription... DataSource.addChangeListener(this.handleChange); } componentWillUnmount() { DataSource.removeChangeListener(this.handleChange); } handleChange() { this.setState({ data: selectData(DataSource, this.props) }); } render() { // ... and renders the wrapped component with the fresh data! // Notice that we pass through any additional props return <WrappedComponent data={this.state.data} {...this.props} />; } }; }
HOC does not modify the input component, nor does it use inheritance to reuse its behavior. HOC is just a function. The wrapped component accepts all the props of the container, and also accepts a new prop (data), which is used to render the output of the wrapped component. HOC doesn't care how the data is used or why the data is used, and the wrapped component doesn't care where the data is obtained.
Because withSubscription is just a regular function, you can add any number of parameters. For example, you can make the name of the data prop configurable to further isolate the HOC from the wrapped component.
Either accept a configuration shouldComponentUpdate, or configure the parameters of the data source
There are some things that need to be paid attention to when using high-order components.
1. Do not modify the original component, this is very important
There are the following examples:
function logProps(InputComponent) { InputComponent.prototype.componentWillReceiveProps = function(nextProps) { console.log('Current props: ', this.props); console.log('Next props: ', nextProps); }; // The fact that we're returning the original input is a hint that it has // been mutated. return InputComponent; } // EnhancedComponent will log whenever props are received const EnhancedComponent = logProps(InputComponent);
There are some problems here, 1. The input component cannot be separated from the enhanced component Reuse. 2. If you apply other HOCs to EnhancedComponent, componentWillReceiveProps will also be changed.
This HOC is not applicable to function type components, because function type components do not have a life cycle. Function HOC should use composition instead of modification - by wrapping the input component into a container component.
function logProps(WrappedComponent) { return class extends React.Component { componentWillReceiveProps(nextProps) { console.log('Current props: ', this.props); console.log('Next props: ', nextProps); } render() { // Wraps the input component in a container, without mutating it. Good! return <WrappedComponent {...this.props} />; } } }
This new logProps has the same functionality as the old logProps, while the new logProps avoids potential conflicts. The same applies to class type components and function type components.
2. Don’t use HOCs in the render method
React’s diff algorithm uses the identity of the component to decide whether it should update the existing subtree or tear down the old subtree and load a new one , if the component returned from the render method is equal to the previously rendered component (===), then React will update the previously rendered component through the diff algorithm. If not, the previously rendered subtree will be completely unloaded.
render() { // A new version of EnhancedComponent is created on every render // EnhancedComponent1 !== EnhancedComponent2 const EnhancedComponent = enhance(MyComponent); // That causes the entire subtree to unmount/remount each time! return <EnhancedComponent />; }
Use HOCs outside the component definition so that the resulting component is only created once. In a few cases, you need to apply HOCs dynamically, you should do this in the life cycle function or constructor
3. Static methods must be copied manually
Sometimes in React It is very useful to define static methods on components. When you apply HOCs to a component, although the original component is wrapped in a container component, the returned new component will not have any static methods of the original component.
// Define a static method WrappedComponent.staticMethod = function() {/*...*/} // Now apply an HOC const EnhancedComponent = enhance(WrappedComponent); // The enhanced component has no static method typeof EnhancedComponent.staticMethod === 'undefined' // true
In order for the returned component to have the static methods of the original component, it is necessary to copy the static methods of the original component to the new component inside the function.
function enhance(WrappedComponent) { class Enhance extends React.Component {/*...*/} // Must know exactly which method(s) to copy :( // 你也能够借助第三方工具 Enhance.staticMethod = WrappedComponent.staticMethod; return Enhance; }
4. The ref on the container component will not be passed to the wrapped component
Although the props on the container component can be easily passed to the wrapped component, But the ref on the container component is not passed to the wrapped component. If you set a ref for a component returned through HOCs, this ref refers to the outermost container component, not the wrapped component.
Related recommendations
React high-order component entry example sharing
How to use Vue high-order component
Use a very simple example to understand the idea of react.js high-order components
The above is the detailed content of React high-order component example analysis. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
