react中this的绑定

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
      class MyComponent extends React.Component {
        // constructor(props) {
        //   super(props);
        //   // 这边绑定是必要的,这样 `this` 才能在回调函数中使用
        //   this.handleClick = this.handleClick.bind(this);
        // }
        // handleClick() {
        //   console.log("haha");
        //   console.log(this);
        //   // 使用原生的 DOM API 获取焦点
        //   //   this.refs.myInput.focus();
        // }

        handleClick = () => {
          console.log("this is:", this);
        };
        render() {
          //  当组件插入到 DOM 后,ref 属性添加一个组件的引用于到 this.refs
          return (
            <div>
              <input type="text" ref="myInput" />
              <input
                type="button"
                value="点我输入框获取焦点"
                onClick={this.handleClick}
              />
            </div>
          );
        }
      }

      ReactDOM.render(<MyComponent />, document.getElementById("example"));
    </script>
  </body>
</html>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
      class MyComponent extends React.Component {
        constructor(props) {
          super(props);
          // 这边绑定是必要的,这样 `this` 才能在回调函数中使用
          this.handleClick = this.handleClick.bind(this);
        }
        handleClick() {
          console.log("haha");
          console.log(this);
          // 使用原生的 DOM API 获取焦点
          //   this.refs.myInput.focus();
        }

        // handleClick = () => {
        //   console.log("this is:", this);
        // };
        render() {
          //  当组件插入到 DOM 后,ref 属性添加一个组件的引用于到 this.refs
          return (
            <div>
              <input type="text" ref="myInput" />
              <input
                type="button"
                value="点我输入框获取焦点"
                onClick={this.handleClick}
              />
            </div>
          );
        }
      }

      ReactDOM.render(<MyComponent />, document.getElementById("example"));
    </script>
  </body>
</html>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
      class MyComponent extends React.Component {
        handleClick() {
          console.log("haha");
          console.log(this);
          // 使用原生的 DOM API 获取焦点
          //   this.refs.myInput.focus();
        }

        // handleClick = () => {
        //   console.log("this is:", this);
        // };
        render() {
          //  当组件插入到 DOM 后,ref 属性添加一个组件的引用于到 this.refs
          return (
            <div>
              <input type="text" ref="myInput" />
              <input
                type="button"
                value="点我输入框获取焦点"
                onClick={this.handleClick.bind(this)}
              />
            </div>
          );
        }
      }

      ReactDOM.render(<MyComponent />, document.getElementById("example"));
    </script>
  </body>
</html>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
      class MyComponent extends React.Component {
        handleClick() {
          console.log("haha");
          console.log(this);
          // 使用原生的 DOM API 获取焦点
          //   this.refs.myInput.focus();
        }

        // handleClick = () => {
        //   console.log("this is:", this);
        // };
        render() {
          //  当组件插入到 DOM 后,ref 属性添加一个组件的引用于到 this.refs
          return (
            <div>
              <input type="text" ref="myInput" />
              <input
                type="button"
                value="点我输入框获取焦点"
                onClick={() => {
                  this.handleClick();
                }}
              />
            </div>
          );
        }
      }

      ReactDOM.render(<MyComponent />, document.getElementById("example"));
    </script>
  </body>
</html>

在这里插入图片描述
这个这个方法本身就在子组件上,所以是子组件触发的,所以我们要改变this的指向,所以要绑定一下。

おすすめ

転載: blog.csdn.net/liulang68/article/details/121531985
おすすめ