1. 程式人生 > 其它 >React.forwardRef 理解

React.forwardRef 理解

React.forwardRef會建立一個React元件,這個元件能夠將其接受的ref屬性轉發到其元件樹下的另一個元件中。這種技術並不常見,但在以下兩種場景中特別有用: 官方解釋: https://react.docschina.org/docs/forwarding-refs.html#forwarding-refs-to-dom-components 個人程式碼例項 理解: 一、refs轉發到DOM
  const { Component, createRef, forwardRef, Fragment } = React
  
  // DOM 元件 const FouseInput = forwardRef((props, ref) => { return (
<input type="text" ref={ref} /> ) }) class App extends Component { componentDidMount () { const { current } = this.inputRef console.log(current); current.focus() } render () { this.inputRef = createRef() return (
<FouseInput ref={this.inputRef}/> ) } }
   ReactDOM.render(<App/>,document.getElementById('app')); 二、refs轉發高階元件

 const{Component,createRef,forwardRef,Fragment}=React
class App extends Component {
      render () {
        const Compoent = LogProps(FouseRefs)
        return (
          
<Compoent/> ) } } class FouseRefs extends Component { render () { const { inputRef, ...rest } = this.props return ( <Fragment> <div>input自動聚焦</div> <input type="text" ref={inputRef} {...rest} /> </Fragment> ) } } // 定義一個高階元件 function LogProps (WrapComponent) { // 接收傳入的元件 並加以處理, 如 繫結ref class WrapCom extends Component { componentDidMount () { const { current } = this.ref console.log(current); current.focus() } render () { this.ref = createRef() return ( <WrapComponent inputRef={this.ref} {...this.props} /> ) } } // 通過forwardRef 包裝 返回出去,即被包裝元件即可使用 ref return forwardRef((props, ref) => { return <WrapCom /> }) } ReactDOM.render(<App />, document.getElementById('app'));
完整程式碼
<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
 
<body>
  <div id="app"></div>
</body>
</html>
  <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>
  <script src="https://cdn.bootcss.com/prop-types/15.6.1/prop-types.js"></script>
  <script type="text/babel"> // 使用jsx語法
    const { Component, createRef, forwardRef, Fragment } = React

    // 一 、refs轉發到DOM
    // const FouseInput = forwardRef((props, ref) => {
    //     return (
    //       <input type="text" ref={ref} />
    //     )
    // })

    // class App extends Component {
    //   componentDidMount () {
    //     const { current } = this.inputRef
    //     console.log(current);
    //     current.focus()
    //   }
    //   render () {
    //     this.inputRef = createRef()
    //     return (
    //       <FouseInput ref={this.inputRef}/>
    //     )
    //   }
    // }

    // 二、refs轉發 高階元件
    class App extends Component {
      render () {
        const Compoent = LogProps(FouseRefs)
        return (
          <Compoent/>
        )
      }
    }

    class FouseRefs extends Component {
      render () {
        const { inputRef, ...rest } = this.props
        return (
          <Fragment>
            <div>input自動聚焦</div>
            <input type="text" ref={inputRef} {...rest} />
          </Fragment>
        )
      }
    }

    // 定義一個高階元件
    function LogProps (WrapComponent) {
      // 接收傳入的元件 並加以處理, 如 繫結ref
      class WrapCom extends Component {
        componentDidMount () {
          const { current } = this.ref
          console.log(current);
          current.focus()
        }
        render () {
          this.ref = createRef()
          return (
            <WrapComponent inputRef={this.ref} {...this.props} />
          )
        }
      }
      // 通過forwardRef 包裝 返回出去,即被包裝元件即可使用 ref
      return forwardRef((props, ref) => {
        return <WrapCom />
      })
    }
    
    ReactDOM.render(<App />, document.getElementById('app'));
  </script>