【发布时间】:2018-11-30 17:04:22
【问题描述】:
我有一个 SideNav 组件,其中包含需要滚动到相邻 html 表 (InfoTable) 中相应标题的动态创建的链接。我尝试了多种不同的方法来实现这一点,但无济于事。
export default class Parent extends Component {
state = {
categories: [],
}
scrollToHeader = (tableRefString) => {
// Function to pass to SideNav to access refs in InfoTable
this[tableRefString].scrollIntoView({ block: 'start' });
}
render() {
return (
<div>
<SideNav
categories={this.state.categories}
scrollToHeader={this.scrollToHeader} />
<InfoTable
categories={this.state.categories} />
</div>
);
}
}
export default class InfoTable extends Component {
render() {
return (
<div>
<table>
<tbody>
{this.props.categories.map(category => (
<>
// Forward the ref through InfoTableHeader to be set on the parent DOM node of each InfoTableHeader
<InfoTableHeader />
{category.inputs.map(input => <InfoTableRow />)}
</>
))}
</tbody>
</table>
</div>
);
}
}
为了单击 SideNav 上的链接并滚动到 InfoTable 上的相应标题,我认为我需要根据类别数组中的名称转发在 Parent 上动态创建的引用并将这些引用设置为 DOM 节点InfoTable 中的每个标题。从那里我会将一个函数传递给 SideNav,该函数可以访问 Parent 中的 refs 以滚动到标题。
- 如何一次将多个引用转发到我的 InfoTable 组件?
- 有没有更简洁的方法来完成我想要做的事情?我研究过 React.findDOMNode() 但 refs 似乎是更好的选择。
【问题讨论】:
-
我正在寻找相同的答案。这有什么好运气吗?
标签: reactjs