Skip to content

Conversation

amanmahajan7
Copy link
Collaborator

If the ref callback is defined as an inline function, it will get called twice during updates, first with null and then again with the DOM element. This is because a new instance of the function is created with each render, so React needs to clear the old ref and set up the new one. You can avoid this by defining the ref callback as a bound method on the class, but note that it shouldn’t matter in most cases.

https://reactjs.org/docs/refs-and-the-dom.html


getHeaderRows = () => {
let rows = [{ ref: (node) => this.row = node, height: this.props.headerRowHeight || this.props.rowHeight, rowType: HeaderRowType.HEADER }];
const rows = [{ height: this.props.headerRowHeight || this.props.rowHeight, rowType: HeaderRowType.HEADER }];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref: this.row is not used anywhere. Header row defines its own ref.

render() {
let headerRows = this.props.headerRows || [{ref: (node) => this.row = node}];
let EmptyRowsView = this.props.emptyRowsView;
const { headerRows } = this.props;
Copy link
Collaborator Author

@amanmahajan7 amanmahajan7 Oct 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/>);
return (
<HeaderRow
key={row.rowType}
Copy link
Collaborator Author

@amanmahajan7 amanmahajan7 Oct 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why was key a function (row.ref is defined in ReactDataGrid.js)

Copy link
Contributor

@malonecj malonecj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@malonecj malonecj merged commit faa8811 into master Oct 31, 2018
@nstepien nstepien deleted the am-replace-inline-refs branch October 31, 2018 15:20
malonecj pushed a commit that referenced this pull request Nov 15, 2018
* Defining the ref callback as a bound method

* Fix unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants