javascript - 在子挂载上访问子组件中的父 Ref
问题描述
我有以下父/子组件。最初呈现 Child 组件后,我需要滚动到顶部。
import React, {Component} from 'react';
import Child from "./Child";
class Parent extends Component {
render() {
return (
<div>
<div ref={topDiv => {
this.topDiv = topDiv;
}}/>
<Child
topDiv={this.topDiv}
/>
</div>
);
}
}
export default Parent;
import React, {Component} from 'react';
class Child extends Component {
componentDidMount() {
this.scrollToTop();
}
// componentWillReceiveProps(nextProps, nextContext) {
// nextProps.topDiv.scrollIntoView();
// }
scrollToTop() {
this.props.topDiv.scrollIntoView();
}
render() {
return (
<div style={{height: '500rem'}}>
Long Child
</div>
);
}
}
export default Child;
使用它,我得到错误:
TypeError:无法读取未定义的属性“scrollIntoView”
我认为这是因为当componentDidMount
被调用时,尚未收到道具,所以topDiv
是 null/undefined。
如果我componentWillReceiveProps
按照注释部分所示使用,它可以正常工作。但我不能使用它,因为:
1. 它已被弃用。
2.我觉得每次收到道具都会调用。所以我想我需要保留一个变量来知道这是否是第一次收到道具?
我不能使用componentDidUpdate
,因为文档说“初始渲染不调用此方法。 ”。
组件渲染后第一次收到 props 怎么办?
解决方案
道具肯定应该在componentDidMount
.
这似乎源于 React 生命周期中的一些混乱。
在您的父母中,您大约在您安装的同时在渲染中提供 ref 回调Child
。根据反应文档:
React 会在组件挂载时使用 DOM 元素调用 ref 回调,并在组件卸载时使用 null 调用它。在 componentDidMount 或 componentDidUpdate 触发之前,保证 Refs 是最新的。
但是,componentDidMount
of 孩子会在他们的父母之前触发componentDidMount
,所以这个保证并不意味着Parent
'sthis.topDiv
将在Child
调用之前定义componentDidMount
。(而且,进一步考虑,它绝对不能保证它会在Child
作为道具提供之前被定义。)
在你的父母中,你可以尝试类似的东西
componentDidMount() {
this.setState({ divSet: true });
}
render() {
let child = null;
if (this.state.divSet) {
child = <Child topDiv={this.topDiv} />
}
return (
<div>
<div ref={topDiv => {
this.topDiv = topDiv;
}}/>
{child}
</div>
);
}
这将保证您的 ref 在Child
安装之前设置。setState
专门用于在设置 ref 时强制父级重新渲染。
推荐阅读
- php - 将 guzzle 响应数据推送到数组并传递给 laravel 中的查看
- steam - 如何获取给定 Steam 应用程序的所有所有者?
- typescript - TypeScript 错误:事件处理程序的“对象”类型上不存在属性“目标”
- ruby - Ruby rspec将带有字符串的模拟哈希转换为带有符号的哈希
- sql - SQL Server 将三个不同的列连接成逗号分隔且没有重复值
- html - 如何解决用于在表中搜索的异步管道中的错误?
- python - logits 和 labels 必须是可广播的,labels 和 logits 维度不匹配
- regex - 如何使用正则表达式提取信息
- php - 在 PHP 中的 mySQL Server 上执行存储过程时如何获取 lastInsertID?
- javascript - 如何在反应中没有eval()的情况下从一个输入计算值