react 記錄

1. 兩種獲取dom節點 的方式

import React, {Fragment} from 'react'

class Test extends React.Component {
  constructor(props) {
    super(props);
    // 創建
    this.third = React.createRef();
  }
  componentDidMount(){
    // note: 不同的方式創建ref, 取值方式不同
    console.log(this.second)            // <input value="second">
    console.log(this.third.current);    //  <input value="third">
  }
  render() {
    // 賦值
    return (
      <Fragment>
        <input value="second" ref={(inputRef) => this.second = inputRef} />
        <input value="third" ref={this.third} />
      </Fragment>
      )

  }
}

export default Test

進階:React.forwardRef

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章