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

Refactored out unsafe_componentWillMount #1388

Merged
merged 1 commit into from
Jul 10, 2020
Merged

Refactored out unsafe_componentWillMount #1388

merged 1 commit into from
Jul 10, 2020

Conversation

patorjk
Copy link
Collaborator

@patorjk patorjk commented Jul 3, 2020

The table currently uses the method unsafe_componentWillMount in the main component. React is removing this lifecycle method in version 17 and it currently gives a warning message to developers using strict mode (#1261). The method is deemed unsafe because it can lead to memory leaks if not used properly.

React recommends using the constructor is initialize state or componentDidMount for initialization that requires DOM nodes. Only the former is relevant to mui-datatables, so the code has been refactored to use the constructor.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.09%) to 71.809% when pulling 2f7cd87 on unsafeMount into 468c9c7 on master.


UNSAFE_componentWillMount() {
this.initializeTable(this.props);
Copy link
Collaborator

Choose a reason for hiding this comment

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

initializeTable has changed the position of componentDidMount.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this an issue?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nope,
I left a trace while reading the code. Sorry. 🙏

}

componentDidMount() {
this.setHeadResizeable(this.resizeHeadCellRefs, this.tableRef);

// When we have a search, we must reset page to view it unless on serverSide since paging is handled by the user.
if (this.props.options.searchText && !this.props.options.serverSide) this.setState({ page: 0 });

this.setTableInit('tableInitialized');
Copy link
Collaborator

Choose a reason for hiding this comment

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

But shouldn't this be the first thing to do?

componentDidMount() {
  this.setTableInit('tableInitialized');

  ...
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To keep consistent, it probably should. I'll move it up.

@@ -677,14 +670,15 @@ class MUIDataTable extends React.Component {
return transformedData;
};

setTableData(props, status, dataUpdated, callback = () => {}) {
setTableData(props, status, dataUpdated, callback = () => {}, fromConstructor = false) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't seem necessary to add fromConstructor.
Because it can be judged by the existence of callback.

And I think it's better to separate the function.

I think it has too heavy functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The constructor sets a default function for that callback. I also wanted to make it explicit at what was happening, since the constructor is the only place where the unique behavior happens.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The constructor sets a default function for that callback. I also wanted to make it explicit at what was happening, since the constructor is the only place where the unique behavior happens.

Oh, I see. There is a default. hmmm...

@wdh2100
Copy link
Collaborator

wdh2100 commented Jul 4, 2020

It is good to remove unsafe_componentWillMount.

it is such a great work!

@wdh2100
Copy link
Collaborator

wdh2100 commented Jul 4, 2020

Now that I've been thinking about it,

it's a refectoring, so you'd better focus logic.

You can take the above reviews lightly.

thanks!

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.

None yet

3 participants