Skip to content

ES6 all React stuff. #140

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

Merged
merged 6 commits into from
Nov 6, 2015
Merged

ES6 all React stuff. #140

merged 6 commits into from
Nov 6, 2015

Conversation

rstudner
Copy link
Contributor

I also cleaned up all the trailing commas (that where everywhere) because they are lint etc issues (rubymine in particular really dislikes them). They hurt nothing to take out.

@justin808 After submitting a comment, which works, there is still an issue (that seems like) a "this.props" where this isn't defined.

You'll notice in the PR various uses of .bind(this).. that is because ES6 react classes don't automagically do that anymore (so like, a clickHandler method cannot refer to "this" (which they all do) unless you do this._handleClick.bind(this) sort of syntax)

Clearly I missed one/haven't unraveled it yet :) That being said, this has to be 95-99% there and this way more eyes can get on it.

@justin808
Copy link
Member

@rstudner Thanks so much! ES6 is great. However, we're trying to keep with the AirBnb JS style guideline as much as possible. The trailing commas are there for a reason.

For the bind parts, we want to use the ES7 :: syntax.

@alexfedoseev anything else?

@justin808
Copy link
Member

Connects to #139

@rstudner
Copy link
Contributor Author

Ahh! my apologies on their removal then! :) I can go put them back in easily enough. (And of course i'm now off reading the AirBNB style guide so I don't do this again etc)

Sorry, I didn't think to 'go ES7' (despite of course the Readme.MD saying ES7 ;). I was mostly just trying to mimic syntax people would see if they were reading the official React docs and weren't familiar with ES7.


propTypes: {
class Comment extends React.Component {
constructor(props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to avoid using constructor in React components, especially since we aren't even setting state here. If we do need to set state, it should be via class a class property. For example, if we want to have a state of `{author: "Timmy", text: "hello world"}, you just do this:

class Comment extends React.Component {
  author = "Timmy";
  text = "hello world";
....

@justin808
Copy link
Member

@rstudner the only CI failure is linting.

@alexfedoseev maybe comment if we should go with this?

@alex35mil
Copy link
Member

@justin808 @rstudner This commit: shakacode/react-editable-grid-example@f7968e1

There are 2 branches:

  • master (with functions, working)
  • fat-arrows (with fat-arrows functions, broken)

Please confirm, that second one is broken.

@rstudner
Copy link
Contributor Author

rstudner commented Nov 2, 2015

Pushed an update to make the lint-robot happy :)

In regards to fat arrow vs :: and other syntax things, the longest(?) explanation I found for them was here:
http://egorsmirnov.me/2015/08/16/react-and-es6-part3.html

@alex35mil
Copy link
Member

The rule is simple: fat arrows works without binding only within one component. And it's broken when they are passed to outer world. When fat arrow functions are used, we have to keep track of the cases when we have to bind function to context and when it's not necessary. So the question is: do we need one more thing to remember?

@alex35mil
Copy link
Member

All in all, I vote for binding methods to context in constructor. One more reason, btw.

@rstudner
Copy link
Contributor Author

rstudner commented Nov 2, 2015

@alexfedoseev - I 100% agree with binding methods in the constructor. Earlier in this (long hah) PR of comments I was told to not use constructors.

Thus -- I went with what I thought was the 'next best' option.

As well, in a Redux world I assumed we wouldn't be doing the 'passing of callback functions down via props' and thus none of these would leak outside of their component of origination.

@alex35mil
Copy link
Member

As well, in a Redux world I assumed we wouldn't be doing the 'passing of callback functions down via props' and thus none of these would leak outside of their component of origination.

@rstudner It happens quite often, for example when we need to pass the callback to save / close reusable modal dialog.

@rstudner
Copy link
Contributor Author

rstudner commented Nov 2, 2015

@alexfedoseev Ahh, i've been handling it differently just on my own personal projects -- but yeah, that would be a widely encountered use case for sure.

Based on your own blog post, which syntax would you go for (you are using .bind(this) in the constructors and as well, earlier in this PR, I was told not to use .bind(this)).

@alex35mil
Copy link
Member

Let's stick with:

constructor(props, context) {
  super(props, context);

  this._handleSomething = this._handleSomething.bind(this);
}

It's less problematic way of doing binding.

We can do something like:

// bindToContext.js
export default methods => {
  methods.forEach(method => this[method] = this[method].bind(this))
}

// Component
constructor(props, context) {
  super(props, context);

  bindToContext([ '_handleSomething' ]).bind(this);
}

@jbhatab @justin808 @robwise @rstudner

@rstudner
Copy link
Contributor Author

rstudner commented Nov 2, 2015

Had a good quick chat with Alex over Slack. Will make some updates in a couple hours (have to feed small childrenBeasts and get them on the bus)

@justin808
Copy link
Member

@rstudner Let's squash and create a nice commit message that sums up the choices. And then we'll merge.

Roger Studner added 2 commits November 2, 2015 15:16
I also renamed the "component internal non-react workflow methods" to have a leading _ per multiple best-practices guides etc.

propTypes: {
this._ajaxCounter = this._ajaxCounter.bind(this);
this._isSendingAjax = this._isSendingAjax.bind(this);
Copy link
Member

Choose a reason for hiding this comment

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

one last change now that we're using lodash:

_.bindAll(this, '_ajaxCounter', '_isSendingAjax');

@rstudner
Copy link
Contributor Author

rstudner commented Nov 5, 2015

Thoughts on the above? I can import lodash in each file, or we can expose it. the above commit just exposes it like react/jquery.

Not sure if this has to be done in the server file (we expose React, but not jquery for example there)

@alex35mil
Copy link
Member

We should expose something only for the reason. For example, React should be exposed for the gem or fetch should be exposed since it's (probably) future global object. There is no reason to expose lodash, so it should be imported.

@rstudner
Copy link
Contributor Author

rstudner commented Nov 5, 2015

Okay, i'll change to imports. Interestingly, exposing it solved 99% of the errors, but not all of them (which is odd all by itself)

justin808 added a commit that referenced this pull request Nov 6, 2015
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.

5 participants