Skip to content

Conversation

@amanmahajan7
Copy link
Collaborator

@amanmahajan7 amanmahajan7 commented Jan 11, 2018

Description

A few sentences describing the overall goals of the pull request's commits.

Migration strategies:
https://reactjs.org/blog/2016/07/13/mixins-considered-harmful.html#migrating-from-mixins

  • Use utility functions when possible

  • If there is just one component using mixin then embed the mixin logic right into the component

  • Higher Order Components are tricky to use as refs are used in many places

  • Remove mixins

    • ScrollShim
    • GridScrollMixin
    • ColumnUtilsMixin
    • ViewportScroll
    • KeyboardHandleMixin
    • DOMMetrics
      • MetricsMixin
      • MetricsComputatorMixin

Please check if the PR fulfills these requirements

What kind of change does this PR introduce? (check one with "x")

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[x] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior? (You can also link to an open issue here)

What is the new behavior?

Does this PR introduce a breaking change? (check one with "x")

[ ] Yes
[ ] No

If this PR contains a breaking change, please describe the impact and migration path for existing applications: ...

Other information:

This reverts commit 320b843.
This reverts commit 4c5d43d.
(ReactDataGrid component adds the MetricsComputatorMixin so mixin methods are available to any child component via the context api, it does not need to be added to the Grid component)
@amanmahajan7 amanmahajan7 changed the title Remove Mixins [WIP] Remove Mixins Jan 13, 2018
@amanmahajan7 amanmahajan7 changed the title [WIP] Remove Mixins Remove Mixins Jan 13, 2018
onHeaderDrop: PropTypes.func
},

mixins: [ColumnUtilsMixin],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does not need to add utility functions as a mixin. They can be used directly

overScan: PropTypes.object
},

mixins: [
Copy link
Collaborator Author

@amanmahajan7 amanmahajan7 Jan 17, 2018

Choose a reason for hiding this comment

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

GridScrollMixin is only used by the Grid component so moved the logic into the Grid component

DOMMetrics.MetricsComputatorMixin defines childContextTypes/getChildContext to implicitly pass metricsComputator through the component tree. ReactDataGrid (Parent) component is using the same mixin and acts as the context provider so Grid component does not need to add it. It can be safely removed

https://reactjs.org/docs/context.html

const ReactDataGrid = createReactClass({
displayName: 'ReactDataGrid',

mixins: [
Copy link
Collaborator Author

@amanmahajan7 amanmahajan7 Jan 17, 2018

Choose a reason for hiding this comment

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

ColumnMetricsMixin is only used by the ReactDataGrid component so moved the logic into the component

DOMMetrics.MetricsComputatorMixin and DOMMetrics.MetricsMixin mixins are used to create a parent child coupling though the unstable context API. The idea is to update children components on window.resize event. The parent component ReactDataGrid maintains a list of components and computator functions and calls metricsUpdated method on each component when the computator function returns a different value. Only two components (ReactDataGrid and Viewport via ViewportScrollMixin) were using this logic and it has been completely removed. The computator logic has been moved to individual components (check resize event handlers)

Some of the KeyboardHandlerMixin functions are moved to keyboardUtils and the remaining functions are moved into the component


const Viewport = createReactClass({
displayName: 'Viewport',
mixins: [ViewportScroll],
Copy link
Collaborator Author

@amanmahajan7 amanmahajan7 Jan 17, 2018

Choose a reason for hiding this comment

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

ViewportScroll helper functions are moved to viewportUtils and the remaining methods are copied into the Viewport component

@amanmahajan7 amanmahajan7 removed the WIP label Jan 18, 2018
@chrisirhc
Copy link

chrisirhc commented Jan 20, 2018

Nice, a great step towards modernizing this towards React 16 and beyond. This should resolve the remaining requirements for moving out of createReactClass.

I'm not sure what tests would be needed for this change though, since all existing behavior should remain the same.

@chrisirhc chrisirhc mentioned this pull request Jan 20, 2018
3 tasks
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.

The strategy here looks good - I am keen to get this in. Perhaps we can add some tests around the utility functions that we migrated. Apart from that I think we are good to go

@amanmahajan7
Copy link
Collaborator Author

amanmahajan7 commented Jan 22, 2018

@malonecj added unit tests for the utility functions

expect(shim.style.width).toBe('5px');
expect(shim.style.height).toBe('10px');
expect(shim.className.indexOf('react-grid-ScrollShim')).not.toBe(-1);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@malonecj
Copy link
Contributor

Awesome work on this one @amanmahajan7. One step closer to React 16

@malonecj malonecj merged commit 94bbd49 into master Jan 25, 2018
@amanmahajan7 amanmahajan7 deleted the am-remove-mixins branch January 25, 2018 19:43
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.

4 participants