1

I am new to the react. Here I have is a table which has some td, now on click of the button I add one more td in that table. Now, the table is also scrollable,so , what I want is that table scroll should be at top when we add a new row. So, that user will come to know easily that the row has been added .

I tried is ,

I have a container,

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}/>
   )
}

UserJobsTable.js

<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>

So, Here I am getting the error that scrollTo is not a function.

So, is there any way to do this ? Or what is it that I am doing wrong.

tanmay
  • 7,761
  • 2
  • 19
  • 38
ganesh kaspate
  • 1
  • 9
  • 41
  • 88

3 Answers3

1

use this.myRef.current?.getNode().scrollTo({ x: 0, y: 0})

Anish Yadav
  • 191
  • 1
  • 4
0

can you please try to change ref={props.refer} from tbody to the div of table? like this : <div className="table-responsive" ref={props.refer}>

as far i know react's scroll to works with div or it needs some plugins like jquery oe window

anyway you can also try this (in the last case fyi this is not a good option): document.body.scrollTo()

for further reference: JavaScript scrollTo method does nothing?

0

because this.myRef get the instance of UserJobsTabel. You can forwarding refs instead.

https://reactjs.org/docs/forwarding-refs.html