转发引用:forwardedRef 始终为空

Har*_*rma 5 javascript reactjs

我有一个Tableref附加到的 ant 设计组件。

我希望能够使用tableRefin HOCwithCustomPagination的生命周期componentDidUpdate方法。

遵循React Docs Forwarding Refs之后,我无法清楚地理解。我可以编写以下代码:

应用程序.js

import WrappedTable from '/path/to/file';

class App extends React.Component {
  render() {
    const tableRef = React.createRef();
    return (
      <WrappedTable ref={tableRef} />
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

表.js

import withCustomPagination from '/path/to/file';

class Table extends React.Component {
  constructor(props) {
    super(props); 
  }

  render() {
    <TableContainer ref={this.props.forwardedRef} />
  }
}

const WrappedTable = withCustomPagination(Table);
export default WrappedTable;
Run Code Online (Sandbox Code Playgroud)

withCustomPagination.js

import CustomPagination from 'path/to/file';

const withCustomPagination = tableRef => Component => {
  class WithCustomPagination extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        rows: 1,
        dataLength: props.dataLength,
      }
    }

    componentDidUpdate() {
      tableRef.current.state ..... //logic using ref, Error for this line
      this.state.rows ..... //some logic
    }

    render() {
      const { forwardedRef } = this.props;
      return (
        <Component {...this.state} ref={forwardedRef} />
        <CustomPagination />
      )
    }
  }
  return React.forwardRef((props, ref) => {
    return <WithCustomPagination {...props} forwardedRef={ref} />;
  });
}

export default withCustomPagination;
Run Code Online (Sandbox Code Playgroud)

调试后,我发现它forwardedRef总是为空。

调试会话截图

Ser*_*ero 2

您的问题发生在您的 HOC 中:

                              here
const withCustomPagination = tableRef => Component => {
Run Code Online (Sandbox Code Playgroud)

您需要删除该参数。访问 ref prop 的方法很简单,componentDidUpdate就像forwardedRefprop 一样,例如:

import CustomPagination from 'path/to/file';

const withCustomPagination = Component => {
  class WithCustomPagination extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        rows: 1,
        dataLength: props.dataLength,
      }
    }

    componentDidUpdate() {
      //You got the ref here
      console.log(forwardedRef.current)
    }

    render() {
      const { forwardedRef } = this.props;
      return (
        <Component {...this.state} ref={forwardedRef} />
        <CustomPagination />
      )
    }
  }
  return React.forwardRef((props, ref) => {
    return <WithCustomPagination {...props} forwardedRef={ref} />;
  });
}

export default withCustomPagination;
Run Code Online (Sandbox Code Playgroud)

还需要考虑的是:

您不应该在 render 方法中创建 ref,因为每次设置状态时都会引发此方法。我建议您在构造函数中执行此操作:

import WrappedTable from '/path/to/file';

class App extends React.Component {
  constructor(){
    super();
    this.reference = React.createRef();
  }
  render() {
    return (
      <WrappedTable ref={this.reference} />
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

另外,在您的 HOC 中仅渲染一个子项或使用React.Fragment. 此外,不要忘记发送其余属性:

const withCustomPagination = Component => {
  class WithCustomPagination extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        rows: 1,
        dataLength: props.dataLength,
      }
    }

    componentDidUpdate() {
      //You got the ref here
      console.log(forwardedRef.current)
    }

    render() {
      // Do not forget to send the rest of properties here like:
      const { forwardedRef, ...rest } = this.props;
      return (
        <React.Fragment>
          <Component {...this.state} ref={forwardedRef} {...rest} />
          <CustomPagination />
        </React.Fragment>
      )
    }
  }
  return React.forwardRef((props, ref) => {
    return <WithCustomPagination {...props} forwardedRef={ref} />;
  });
}

export default withCustomPagination;
Run Code Online (Sandbox Code Playgroud)

编辑:

添加 ref 属性的引用

import withCustomPagination from '/path/to/file';

class Table extends React.Component {
  constructor(props) {
    super(props); 
    this.reference = React.createRef();
  }

  render() {
    <TableContainer ref={this.reference} />
  }
}

const WrappedTable = withCustomPagination(Table);
export default WrappedTable;
Run Code Online (Sandbox Code Playgroud)