Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining the ref callback as a bound method #1353

Merged
merged 3 commits into from
Oct 31, 2018
Merged

Conversation

amanmahajan7
Copy link
Contributor

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

@@ -558,10 +558,9 @@ class ReactDataGrid extends React.Component {
};

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