table icon indicating copy to clipboard operation
table copied to clipboard

Scroll position resetting to 0 on re-render

Open pourmesomecode opened this issue 8 years ago • 5 comments

Using v6.8.0

This is someone else pen, but it works as an example: https://codepen.io/Kamilius/pen/LpObwW?editors=0010

Scroll half way. Change someone's name that's visible in the viewport. Table re-renders and scroll position resets to 0.

If I have a dynamically created table, with a lot of records, while I'm viewing this table, a new record is created, the scroll position is reset to 0. I'd then need to scroll back down to my previous position.

React by default doesn't change the scroll position. This must be a custom function in react-table is there any way to override/disable this behavior? So upon updating the table with a new record I'm not forced to scroll to my previous position?

pourmesomecode avatar May 11 '18 09:05 pourmesomecode

ReactTable is a page-based table and isn't optimised for scrolling.

gary-menzel avatar May 11 '18 11:05 gary-menzel

I'm having the same problem, did you manage to fix this?

@gary-menzel is there any way to prevent this re-rendering of all of the rows?

SelaO avatar May 15 '18 12:05 SelaO

@pourmesomecode We managed to fix this, we overrode the body component with a function at first, but when we changed it to be a component it fixed it.

i.e:

<ReactTable
          data={data}
          TbodyComponent={BodyComponent}
....

class BodyComponent extends Component {
  render() {
    return (
      <myDiv>
        {ReactTableUtils.makeTemplateComponent('rt-tbody', 'Tbody')(this.props)}
      </myDiv>
    )
  }
}

SelaO avatar May 15 '18 13:05 SelaO

Closing due to issue age. If this issue was a question, please ask it again on https://spectrum.chat/react-table. If you think this issue needs to be reopened or if it should be turned into a pull-request, please check the latest version of React-Table for the issue or feature once more and reopen or create a PR if appropriate. Thanks!

tannerlinsley avatar May 24 '18 22:05 tannerlinsley

Came here after running into this issue in 6.7.0. In my case, if React-Table becomes invisible (e.g. in a tabbed interface) and then visible again, it retains the selected row, but fails to retain the scroll position. I worked around this way for now (typescript):

  private scrollSelectedIntoView() {
    const table = document.getElementsByClassName("ReactTable")[0];
    table
      .getElementsByClassName("rt-tr selected")[0]
      .scrollIntoView({ block: "nearest" });
  }

<ReactTable
    ....
       onFetchData={() => this.scrollSelectedIntoView()}

This may well be an abuse of the onFetchData event, happy to hear of a better way.

hatton avatar Dec 26 '18 17:12 hatton