Javascript react scrollTo不是一个函数
我是新来的。这里我有一个表,其中有一些Javascript react scrollTo不是一个函数,javascript,reactjs,redux,scroll,react-redux,Javascript,Reactjs,Redux,Scroll,React Redux,我是新来的。这里我有一个表,其中有一些td,现在点击按钮,我在该表中添加了一个以上的td。现在,表格也可以滚动,所以,我想要的是,当我们添加新行时,表格滚动应该在顶部。因此,该用户将很容易知道该行已被添加 我试过的是 我有一个容器 JobList.js constructre(props){ super(props); this.myRef = React.createRef(); } componentDidMount() { if (this.state.opera
td
,现在点击按钮,我在该表中添加了一个以上的td。现在,表格也可以滚动,所以,我想要的是,当我们添加新行时,表格滚动应该在顶部。因此,该用户将很容易知道该行已被添加
我试过的是
我有一个容器
JobList.js
constructre(props){
super(props);
this.myRef = React.createRef();
}
componentDidMount() {
if (this.state.operationType) {
this.props.changeOperationType(this.state.operationType);
}
if (!this.props.jobs) {
this.props.fetchUserJd();
}
this.myRef.current.scrollTo(0, 0);
}
render() {
return(
<UserJobsTabel
jobList={filteredList}
sortAscending={this.sortData}
sortCountAndScoreAscending={this.sortNumbersAscending}
addNewRow={this.addNewRow}
isRowAddingEditorVisible={this.props.isRowAddingEditorVisible}
removeRow={this.removeRow}
refer={this.myRef}/>
)
}
<div className="table-responsive">
<table>
<thead>
<tr className="text-center">
<th></th>
<th scope="col">Technology<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Total Resumes<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Job Title<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Total Score<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Average Score<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th></th>
</tr>
</thead>
<tbody className="text-center" ref={props.refer}>
</tbody>
//remaining
</div>
constructure(道具){
超级(道具);
this.myRef=React.createRef();
}
componentDidMount(){
if(this.state.operationType){
this.props.changeOperationType(this.state.operationType);
}
如果(!this.props.jobs){
this.props.fetchUserJd();
}
this.myRef.current.scrollTo(0,0);
}
render(){
返回(
)
}
UserJobsTable.js
constructre(props){
super(props);
this.myRef = React.createRef();
}
componentDidMount() {
if (this.state.operationType) {
this.props.changeOperationType(this.state.operationType);
}
if (!this.props.jobs) {
this.props.fetchUserJd();
}
this.myRef.current.scrollTo(0, 0);
}
render() {
return(
<UserJobsTabel
jobList={filteredList}
sortAscending={this.sortData}
sortCountAndScoreAscending={this.sortNumbersAscending}
addNewRow={this.addNewRow}
isRowAddingEditorVisible={this.props.isRowAddingEditorVisible}
removeRow={this.removeRow}
refer={this.myRef}/>
)
}
<div className="table-responsive">
<table>
<thead>
<tr className="text-center">
<th></th>
<th scope="col">Technology<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Total Resumes<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Job Title<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Total Score<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th scope="col">Average Score<i className="fa fa-fw fa-sort sort-icon"></i></th>
<th></th>
</tr>
</thead>
<tbody className="text-center" ref={props.refer}>
</tbody>
//remaining
</div>
技术
总简历
职位名称
总分
平均分
//剩余的
所以,这里我得到了一个错误,scrollTo不是一个函数
那么,有没有办法做到这一点?或者我做错了什么。您能尝试将ref={props.reference}从tbody更改为表的div吗? 这样地:
据我所知,react的滚动条可以与div一起使用,或者它需要一些插件,比如jQueryOE窗口
无论如何,您也可以尝试以下方法(在最后一种情况下,仅供参考,这不是一个好的选择):
document.body.scrollTo()
供进一步参考:
您能尝试将ref={props.refere}从tbody更改为table的div吗? 这样地:
据我所知,react的滚动条可以与div一起使用,或者它需要一些插件,比如jQueryOE窗口
无论如何,您也可以尝试以下方法(在最后一种情况下,仅供参考,这不是一个好的选择):
document.body.scrollTo()
供进一步参考:
因为
this.myRef
获取UserJobsTabel
的实例。您可以改为转发引用
因为
this.myRef
获取UserJobsTabel
的实例。您可以改为转发引用
使用
this.myRef.current?.getNode().scrollTo({x:0,y:0})
使用this.myRef.current?.getNode().scrollTo({x:0,y:0})
this.myRef
定义在哪里?要使用新的ref系统,必须在构造函数中调用this.myRef=React.createRef()代码>我已将其添加到构造函数中,或者@dotconnor已更新questtion@tanmay很抱歉,我没有找到您,而是调用了scrollTo
function assignscrollTop
param。比如this.myRef.current.scrollTop=0
在哪里定义了this.myRef
?要使用新的ref系统,必须在构造函数中调用this.myRef=React.createRef()代码>我已将其添加到构造函数中,或者@dotconnor已更新questtion@tanmay很抱歉,我没有找到您,而是调用了scrollTo
function assignscrollTop
param。像this.myRef.current.scrollTop=0
这就是为什么最后一个选项是document.body.scrollTo(),这就是为什么最后一个选项是document.body.scrollTo()