转发 Refs:forwardedRef 始终为 null
我有一个 ant design
Table
组件,我想将
ref
附加到该组件。
我希望能够在 HOC
withCustomPagination
的生命周期
componentDidUpdate
方法中使用
tableRef
。
遵循 React Docs Forwarding Refs ,我无法清楚地理解。我可以编写以下代码:
App.js
import WrappedTable from '/path/to/file';
class App extends React.Component {
render() {
const tableRef = React.createRef();
return (
<WrappedTable ref={tableRef} />
)
}
}
Table.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;
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;
调试后,我发现
forwardedRef
始终为空。
您的问题发生在您的 HOC 中:
here
const withCustomPagination = tableRef => Component => {
您需要删除该参数。访问 ref prop 的方法很简单,只需在您的
componentDidUpdate
方法中,例如
forwardedRef
prop:
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;
还需要考虑以下几点:
您不应在 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} />
)
}
}
此外,在您的 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;
编辑:
添加 ref prop 的引用
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;
为了在 HOC
withCustomPagination
中访问
tableRef
,我从
App.js
中删除了
const tableRef = React.createRef()
以及相应的
ref = {tableRef
行。
我将
tableRef
传递给 HOC,并对其进行了柯里化,即
withCustomPagination(tableRef)(NewGiftCardTable)
。我还删除了 HOC 中的所有
Forwarding Refs
逻辑,这样做是因为我只需要在 HOC 中访问
tableRef
,而不是在
App.js
中。
将上述删除的行添加到 Table.js :
import withCustomPagination from '/path/to/file';
const tableRef = React.createRef();
class Table extends React.Component {
constructor(props) {
super(props);
}
render() {
<TableContainer ref={tableRef} />
}
const WrappedTable = withCustomPagination(tableRef)(Table);
export default WrappedTable;