[React] Reference a node using createRef() in React 16.3

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React] Reference a node using createRef() in React 16.3相关的知识,希望对你有一定的参考价值。

In this lesson, we look at where we came from with refs in React. Starting with the deprecated string ref pattern, callback refs, and then how to use the new createRef() method in React 16.3.

Additional Resources: refs and the dom

 

You can use ‘React.createRef()‘ to create a ref object. Then to access it from <obj>.current.<ref_name>

import React from "react";
import { render } from "react-dom";

class App extends React.Component {
  fullName = React.createRef();

  handleBlur = () => {
    this.fullName.current.blur();
  };

  handleFocus = () => {
    this.fullName.current.focus();
  };

  render() {
    return (
      <div className="section">
        <div className="field">
          <label className="label">Full Name</label>
          <div className="control">
            <input className="input" ref={this.fullName} type="text" />
          </div>
        </div>
        <button
          className="button is-link is-outlined"
          onClick={this.handleFocus}
        >
          Focus
        </button>{" "}
        <button
          className="button is-danger is-outlined"
          onClick={this.handleBlur}
        >
          Blur
        </button>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

 

以上是关于[React] Reference a node using createRef() in React 16.3的主要内容,如果未能解决你的问题,请参考以下文章

React interrelated reference Manual

React interrelated reference Manual

node.js学习之react,redux,react-redux

/// <reference types="node" /> 是啥意思?

windows 下 node 安装 react

ERROR in D:/project/node_modules/@types/node/index.d.ts (20,1): Invalid‘ reference‘ directive syntax