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

Components: Turn a few into Component classes #19102

Merged
merged 14 commits into from
Oct 26, 2017

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Oct 24, 2017

Modeled after #19085. I grepped for this.getInitialState() and did the following (same as #19085 (comment)):

  • Temporarily replace the this.getInitialState() call with a dummy (I used a string, 'intialState' 😄 )
  • Replace createReactClass with React.createClass so the codemod would pick it up
  • Run the codemod
  • Introduce the intialState static, use it to init state, and replace the dummy 'initialState' string with it.

To test: Verify that the affected components still work as before.

@ockham ockham added Components [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Oct 24, 2017
@ockham ockham self-assigned this Oct 24, 2017
@matticbot
Copy link
Contributor

@ockham ockham requested review from sirreal and samouri October 24, 2017 12:02
// initialState is also used to reset state when a the taxonomy prop changes
static initialState = {
searchTerm: '',
requestedPages: [ 1 ],
Copy link
Member

Choose a reason for hiding this comment

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

The operations on this.state.requestedPages seem to be safe, but with this change I'd advocate using Object.freeze on any array/object in Component.defaultProps or Component.initialState.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

analyticsPrefix: 'Category Selector',
searchThreshold: 8,
loading: true,
terms: [],
Copy link
Member

Choose a reason for hiding this comment

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

Want to Object.freeze this too?

Copy link
Contributor Author

@ockham ockham Oct 24, 2017

Choose a reason for hiding this comment

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

Oops, oversight of mine. ccd4ee7

@ockham
Copy link
Contributor Author

ockham commented Oct 24, 2017

Discovered something pre-existing that didn't seem right: 2811098

@@ -3,10 +3,8 @@
*
* @format
Copy link
Member

Choose a reason for hiding this comment

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

Want to pull @format out of external deps?

I've looked over this file and smoke tested via the post editor. Looks good 👍

(a review in parts)

@@ -3,9 +3,8 @@
*
Copy link
Member

Choose a reason for hiding this comment

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

I smoke tested followers-data and email-followers-data and there seem to be no regressions 👍

Interesting, first time I've seen the DataComponent wrapper in Calypso 🤔

@@ -3,9 +3,8 @@
*
Copy link
Member

Choose a reason for hiding this comment

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

I smoke tested followers-data and email-followers-data and there seem to be no regressions 👍

Interesting, first time I've seen the DataComponent wrapper in Calypso 🤔

Also fixes an incorrect component name in README
@@ -3,9 +3,8 @@
*
Copy link
Member

Choose a reason for hiding this comment

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

👍 Smoke tested and learned about viewers (only available on private, non-Jetpack sites).

@sirreal
Copy link
Member

sirreal commented Oct 26, 2017

I pushed a commit which names exported default classes. The names were lost since displayName removal, but can now be read via class names.

highlightedIndex: -1,
showSearch: false,
isKeyboardEngaged: false,
};

state = this.constructor.initialState;

reset() {
Copy link
Member

@sirreal sirreal Oct 26, 2017

Choose a reason for hiding this comment

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

I was trying to trigger this to confirm correct behavior, but it appears to be unused. Can you find whether this method is referenced anywhere? We might be able to simplify this further and not need to make the initialState changes.

Looks like this was introduced in #5808 by @marekhrabe (8b08a8f).

Can you say whether it's safe to remove this method which doesn't appear to be called?

👍 I'd like to rip it out if possible, but this file looks good either way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reset is not in use right now. It was part of work for #8739 that wasn't implemented. I think it's safe to remove now and simplify the initialState declaration. It can be easily reintroduced once we are sure we need it.

@@ -9,10 +9,10 @@ A component that fetches a private wpcom site's viewers and passes them to its c

## Usage

A component wrapped with `<FollowersData />` will receive the following props:
Copy link
Member

Choose a reason for hiding this comment

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

Just fixing wrong component reference.

@@ -3,15 +3,12 @@
*
Copy link
Member

Choose a reason for hiding this comment

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

Smoke tested, works well 👍

@@ -3,10 +3,8 @@
*
Copy link
Member

Choose a reason for hiding this comment

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

👍 smoke tested

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

💪 Everything seems to work well, nicely done!

It would be nice to completely remove the reset and the initialState it requires if we can determine that's safe.

@marekhrabe
Copy link
Contributor

Great work on this PR! 💖

@ockham
Copy link
Contributor Author

ockham commented Oct 26, 2017

Thanks a lot for reviewing @sirreal and for chiming in @marekhrabe!

It would be nice to completely remove the reset and the initialState it requires if we can determine that's safe.

907d961

@ockham ockham merged commit e84d803 into master Oct 26, 2017
@ockham ockham deleted the update/react-component-classes branch October 26, 2017 19:35
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 26, 2017
rclations pushed a commit to rclations/wp-calypso that referenced this pull request Nov 15, 2017
Most of these weren't migrated by the `create-class` codemod before because they contained a reference to `this.getInitialState()` in some method (to reset the component to its initial state after some user interaction). This can be resolved by defining `static initialState = {...}; state = this.constructor.initialState;`, and replacing the reference to `this.getInitialState()` by `this.constructor.initialState`.

Since `SiteSelector` wasn't actually using the `reset()` method which referenced `this.getInitialState()`, that method was removed altogether (props @sirreal!).

Finally, this PR fixes an incorrect component name in `<ViewersData />`'s README, as well as the affected components' `@format` comment headers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants