Consider the following rendered ReactJS component:
render () {
return (
<div className='ux-table' onScroll={this.handleScroll}>
<table ref='mainTable>
<Header>
**Rows data here***
</table>
</div>
);
And the Header child component:
render () {
return (
<thead ref='tableHeader'>
<tr> ** column data here ** </tr>
</thead>
);
}
I need to, on the main component handle, get the scrollTop position of the main component (mainTable) and set it to the child Header component (tableHeader), as the following Javascript code:
document.querySelector('.ux-table').onscroll = function (e) {
// called when the window is scrolled.
var topOfDiv = Math.max(document.querySelector(".ux-table").scrollTop - 2, 0);
document.getElementsByTagName('thead')[0].style = "top:" + topOfDiv + "px;";
}
My try on main component:
handleScroll = (event) => {
var topOfDiv = Math.max(this.refs.mainTable.scrollTop - 2, 0);
this.refs.tableHeader.scrollTop = topOfDiv;
}
In first line I´m getting zero (0) for this.refs.mainTable.scrollTop. On second line, I´m getting an error as I cannot access the subcomponent directly.
In short, how to:
a) Read and set the scrollTop attribute of a React component using ref
b) Get access to a child component from its parent ref
Thanks for helping.
refusage....