gpt4 book ai didi

javascript - 转发引用 : forwardedRef is always null

转载 作者:行者123 更新时间:2023-11-30 14:13:27 24 4
gpt4 key购买 nike

我有一个 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一直为null。

Debug session screenshot

最佳答案

您的问题发生在您的 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;

还需要考虑的是:

您不应该在渲染方法中创建 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 中只渲染一个 child 或使用 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 属性的引用

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;

关于javascript - 转发引用 : forwardedRef is always null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com