JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Two ways to implement React's ref callback function

Author:JIYIK Last Updated:2025/03/02 Views:

In the article "React component refs detailed explanation" , we explained the use scenarios and methods of ref. One example is given: using an event to make an input element gain focus.

We also borrow this example here. In the original example, we used the ref string method. In this article, we will use the callback function method to implement it.

ES6 callback functions

Here we use ES6 callback function to get focus

var MyComponent = React.createClass({
    handleClick: function() {
        // Explicitly focus the text input using the raw DOM API.
        if (this.myTextInput !== null) {
            this.myTextInput.focus();
        }
    },
    render: function() {
      return (
            <div>
                <input type="text" ref={ (ref)=>this.myTextInput = ref } />
                <input
                    type="button"
                    value="Focus the text input"
                    onClick={this.handleClick}
                    />
            </div>
        );
    }
});

CommonJs callback function implementation

var MyComponent = React.createClass({
    handleClick: function() {
        // Explicitly focus the text input using the raw DOM API.
        if (this.myTextInput !== null) {
            this.myTextInput.focus();
        }
    },
    render: function() {
      return (
            <div>
                <input type="text" ref={ function(ref){this.myTextInput = ref}.bind(this) } />
                <input
                    type="button"
                    value="Focus the text input"
                    onClick={this.handleClick}
                    />
            </div>
        );
    }
});

Note: In the above code, CommonJs syntax is used, and there is .bind(this) after the callback function function(){}. This is something that needs attention. Bind this so that the this object in the function is the component. If this is not bound, then this.myTextInput in handleClick will report an undefined error. This is something that needs attention. This problem does not exist in ES6.

The purpose of this article is to introduce how to use the ref callback function through examples. I hope this article will be helpful to everyone.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:176 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

React component refs detailed explanation

Publish Date:2025/03/02 Views:118 Category:React

React component reference ref can be said to be a component identifier. Ref is commonly used to make input get focus. Because render returns only virtual DOM, ref is used here.

Common terms used in React Virtual DOM

Publish Date:2025/03/02 Views:174 Category:React

In all the terms of React, there are five core types that we need to remember. These five types are ReactElement / ReactElement Factory ReactNode ReactComponent / ReactComponent Class

React Getting Started createClass Usage Instructions

Publish Date:2025/03/02 Views:50 Category:React

Before using React.createClass, lets look at the official explanation ReactClass createClass(object specification) Create a component class with a given specification (the specification is the object specification). This component implement

React.createElement method usage details

Publish Date:2025/03/02 Views:193 Category:React

The React.createElement method creates and returns a ReactElement element of a given type. The type parameter can be an html tag name string or a ReactClasss. This type parameter is required for createElement.

React Factory Method - Detailed Explanation of createFactory

Publish Date:2025/03/02 Views:88 Category:React

React.createFactory returns a function for generating ReactElement of a given type, similar to React.createElement. The type parameter can be an html tag name (for example: "div", "li", etc.) or a ReactClass object.

React Project Management

Publish Date:2025/03/02 Views:123 Category:React

In the article "Beginner's Guide to React - Building a React Runtime Environment", we only introduced how to build a React runtime environment, which is actually how to reference the React library. But I think it is still a bit confusing for

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial